Example #1
0
 /**
  * Reorders the message icons from a drag/drop event
  */
 public function action_messageiconorder()
 {
     global $context, $txt;
     // Initilize
     $context['xml_data'] = array();
     $errors = array();
     $order = array();
     // Seems these will be needed
     loadLanguage('Errors');
     loadLanguage('ManageSmileys');
     require_once SUBSDIR . '/MessageIcons.subs.php';
     // You have to be allowed to do this
     $validation_token = validateToken('admin-sort', 'post', true, false);
     $validation_session = validateSession();
     if (empty($validation_session) && $validation_token === true) {
         // No questions that we are reordering
         if (isset($_POST['order']) && $_POST['order'] == 'reorder') {
             // Get the current list of icons.
             $message_icons = fetchMessageIconsDetails();
             $view_order = 0;
             $iconInsert = array();
             // The field ids arrive in 1-n view order, so we simply build an update array
             foreach ($_POST['list_message_icon_list'] as $id) {
                 $iconInsert[] = array($id, $message_icons[$id]['board_id'], $message_icons[$id]['title'], $message_icons[$id]['filename'], $view_order);
                 $view_order++;
             }
             // With the replace set
             if (!empty($iconInsert)) {
                 updateMessageIcon($iconInsert);
                 sortMessageIconTable();
             } else {
                 $errors[] = array('value' => $txt['no_sortable_items']);
             }
         }
         $order[] = array('value' => $txt['icons_reordered']);
     } else {
         if (!empty($validation_session)) {
             $errors[] = array('value' => $txt[$validation_session]);
         }
         if (empty($validation_token)) {
             $errors[] = array('value' => $txt['token_verify_fail']);
         }
     }
     // New generic token for use
     createToken('admin-sort', 'post');
     $tokens = array(array('value' => $context['admin-sort_token'], 'attributes' => array('type' => 'token')), array('value' => $context['admin-sort_token_var'], 'attributes' => array('type' => 'token_var')));
     // Return the response
     $context['sub_template'] = 'generic_xml';
     $context['xml_data'] = array('orders' => array('identifier' => 'order', 'children' => $order), 'tokens' => array('identifier' => 'token', 'children' => $tokens), 'errors' => array('identifier' => 'error', 'children' => $errors));
 }
    /**
     * Allows to edit the message icons.
     */
    public function action_editicon()
    {
        global $context, $settings, $txt, $scripturl;
        require_once SUBSDIR . '/MessageIcons.subs.php';
        // Get a list of icons.
        $context['icons'] = fetchMessageIconsDetails();
        // Submitting a form?
        if (isset($_POST['icons_save'])) {
            checkSession();
            // Deleting icons?
            if (isset($_POST['delete']) && !empty($_POST['checked_icons'])) {
                $deleteIcons = array();
                foreach ($_POST['checked_icons'] as $icon) {
                    $deleteIcons[] = (int) $icon;
                }
                // Do the actual delete!
                deleteMessageIcons($deleteIcons);
            } elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon'])) {
                $_GET['icon'] = (int) $_GET['icon'];
                // Do some preperation with the data... like check the icon exists *somewhere*
                if (strpos($_POST['icon_filename'], '.png') !== false) {
                    $_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4);
                }
                if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.png')) {
                    fatal_lang_error('icon_not_found');
                } elseif (strlen($_POST['icon_filename']) > 16) {
                    fatal_lang_error('icon_name_too_long');
                } elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon'])) {
                    fatal_lang_error('icon_after_itself');
                }
                // First do the sorting... if this is an edit reduce the order of everything after it by one ;)
                if ($_GET['icon'] != 0) {
                    $oldOrder = $context['icons'][$_GET['icon']]['true_order'];
                    foreach ($context['icons'] as $id => $data) {
                        if ($data['true_order'] > $oldOrder) {
                            $context['icons'][$id]['true_order']--;
                        }
                    }
                }
                // If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql)
                if (empty($_GET['icon']) && empty($context['icons'])) {
                    $_GET['icon'] = 1;
                }
                // Get the new order.
                $newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1;
                // Do the same, but with the one that used to be after this icon, done to avoid conflict.
                foreach ($context['icons'] as $id => $data) {
                    if ($data['true_order'] >= $newOrder) {
                        $context['icons'][$id]['true_order']++;
                    }
                }
                // Finally set the current icon's position!
                $context['icons'][$_GET['icon']]['true_order'] = $newOrder;
                // Simply replace the existing data for the other bits.
                $context['icons'][$_GET['icon']]['title'] = $_POST['icon_description'];
                $context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename'];
                $context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board'];
                // Do a huge replace ;)
                $iconInsert = array();
                $iconInsert_new = array();
                foreach ($context['icons'] as $id => $icon) {
                    if ($id != 0) {
                        $iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
                    } else {
                        $iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
                    }
                }
                updateMessageIcon($iconInsert);
                if (!empty($iconInsert_new)) {
                    addMessageIcon($iconInsert_new);
                }
            }
            // Sort by order, so it is quicker :)
            sortMessageIconTable();
            // Unless we're adding a new thing, we'll escape
            if (!isset($_POST['add'])) {
                redirectexit('action=admin;area=smileys;sa=editicons');
            }
        }
        $context[$context['admin_menu_name']]['current_subsection'] = 'editicons';
        $token = createToken('admin-sort');
        $listOptions = array('id' => 'message_icon_list', 'title' => $txt['icons_edit_message_icons'], 'sortable' => true, 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons', 'get_items' => array('function' => array($this, 'list_fetchMessageIconsDetails')), 'no_items_label' => $txt['icons_no_entries'], 'columns' => array('icon' => array('data' => array('sprintf' => array('format' => '<img src="%1$s" alt="%2$s" />', 'params' => array('image_url' => false, 'filename' => true)), 'class' => 'centertext')), 'filename' => array('header' => array('value' => $txt['smileys_filename']), 'data' => array('sprintf' => array('format' => '%1$s.png', 'params' => array('filename' => true)))), 'tooltip' => array('header' => array('value' => $txt['smileys_description']), 'data' => array('db_htmlsafe' => 'title')), 'board' => array('header' => array('value' => $txt['icons_board']), 'data' => array('db' => 'board')), 'modify' => array('header' => array('value' => $txt['smileys_modify']), 'data' => array('sprintf' => array('format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>', 'params' => array('id' => false)))), 'check' => array('header' => array('value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />', 'class' => 'centertext'), 'data' => array('sprintf' => array('format' => '<input type="checkbox" name="checked_icons[]" value="%1$d" class="input_check" />', 'params' => array('id' => false)), 'class' => 'centertext'))), 'form' => array('href' => $scripturl . '?action=admin;area=smileys;sa=editicons', 'hidden_fields' => array('icons_save' => 1)), 'additional_rows' => array(array('position' => 'below_table_data', 'class' => 'submitbutton', 'value' => '
						<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" onclick="return confirm(\'' . $txt['icons_confirm'] . '\');" class="right_submit" />
						<a class="linkbutton" href="' . $scripturl . '?action=admin;area=smileys;sa=editicon">' . $txt['icons_add_new'] . '</a>'), array('position' => 'after_title', 'value' => $txt['icons_reorder_note'])), 'javascript' => '
				$().elkSortable({
					sa: "messageiconorder",
					error: "' . $txt['admin_order_error'] . '",
					title: "' . $txt['admin_order_title'] . '",
					placeholder: "ui-state-highlight",
					href: "?action=admin;area=smileys;sa=editicons",
					token: {token_var: "' . $token['admin-sort_token_var'] . '", token_id: "' . $token['admin-sort_token'] . '"}
				});
			');
        require_once SUBSDIR . '/GenericList.class.php';
        createList($listOptions);
        // If we're adding/editing an icon we'll need a list of boards
        if ($context['sub_action'] == 'editicon' || isset($_POST['add'])) {
            // Force the sub_template just in case.
            $context['sub_template'] = 'editicon';
            $context['new_icon'] = !isset($_GET['icon']);
            // Get the properties of the current icon from the icon list.
            if (!$context['new_icon']) {
                $context['icon'] = $context['icons'][$_GET['icon']];
            }
            // Get a list of boards needed for assigning this icon to a specific board.
            $boardListOptions = array('selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0);
            require_once SUBSDIR . '/Boards.subs.php';
            $context += getBoardList($boardListOptions);
        }
    }