Example #1
0
 /**
  * Reorders the smileys from a drag/drop event
  * Will move them from post to popup location and visa-versa
  * Will move them to new rows
  */
 public function action_smileyorder()
 {
     global $context, $txt;
     // Start off with an empty response
     $context['xml_data'] = array();
     $errors = array();
     $order = array();
     // Chances are I wear a silly ;D
     loadLanguage('Errors');
     loadLanguage('ManageSmileys');
     require_once SUBSDIR . '/Smileys.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) {
         // Valid posting
         if (isset($_POST['order']) && $_POST['order'] == 'reorder') {
             // Get the details on the moved smile
             list(, $smile_moved) = explode('_', $_POST['moved']);
             $smile_moved = (int) $smile_moved;
             $smile_moved_details = getSmiley($smile_moved);
             // Check if we moved rows or locations
             $smile_received_location = null;
             $smile_received_row = null;
             if (!empty($_POST['received'])) {
                 $displayTypes = array('postform' => 0, 'popup' => 2);
                 list($smile_received_location, $smile_received_row) = explode('|', $_POST['received']);
                 $smile_received_location = $displayTypes[substr($smile_received_location, 7)];
             }
             // If these are not set, we are kind of lost :P
             if (isset($smile_received_location, $smile_received_row)) {
                 // Read the new ordering, remember where the moved smiley is in the stack
                 $list_order = 0;
                 $moved_key = 0;
                 $smiley_tree = array();
                 foreach ($_POST['smile'] as $smile_id) {
                     $smiley_tree[] = $smile_id;
                     // Keep track of where the moved smiley is in the sort stack
                     if ($smile_id == $smile_moved) {
                         $moved_key = $list_order;
                     }
                     $list_order++;
                 }
                 // Now get the updated row, location, order
                 $smiley = array();
                 $smiley['row'] = !isset($smile_received_row) ? $smile_moved_details['row'] : $smile_received_row;
                 $smiley['location'] = !isset($smile_received_location) ? $smile_moved_details['location'] : $smile_received_location;
                 $smiley['order'] = -1;
                 // If the node after the drop zone is in the same row/container, we use its position
                 if (isset($smiley_tree[$moved_key + 1])) {
                     $possible_after = getSmiley($smiley_tree[$moved_key - 1]);
                     if ($possible_after['row'] == $smiley['row'] && $possible_after['location'] == $smiley['location']) {
                         $smiley = getSmileyPosition($smiley['location'], $smiley_tree[$moved_key - 1]);
                     }
                 }
                 // Empty means getSmileyPosition failed and so do we
                 if (!empty($smiley)) {
                     moveSmileyPosition($smiley, $smile_moved);
                     // Done with the move, now we clean up across the containers/rows
                     $smileys = getSmileys();
                     foreach (array_keys($smileys) as $location) {
                         foreach ($smileys[$location]['rows'] as $id => $smiley_row) {
                             // Fix empty rows if any.
                             if ($id != $smiley_row[0]['row']) {
                                 updateSmileyRow($id, $smiley_row[0]['row'], $location);
                                 // Only change the first row value of the first smiley.
                                 $smileys[$location]['rows'][$id][0]['row'] = $id;
                             }
                             // Make sure the smiley order is always sequential.
                             foreach ($smiley_row as $order_id => $smiley) {
                                 if ($order_id != $smiley['order']) {
                                     updateSmileyOrder($smiley['id'], $order_id);
                                 }
                             }
                         }
                     }
                     // Clear the cache, its stale now
                     cache_put_data('parsing_smileys', null, 480);
                     cache_put_data('posting_smileys', null, 480);
                     $order[] = array('value' => $txt['smileys_moved_done']);
                 }
             }
         } else {
             $errors[] = array('value' => $txt['smileys_moved_fail']);
         }
     } 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, whatever it is
     $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 smileys order.
  */
 public function action_setorder()
 {
     global $context, $txt;
     require_once SUBSDIR . '/Smileys.subs.php';
     $context['sub_template'] = 'setorder';
     // Move smileys to another position.
     if (isset($_REQUEST['reorder'])) {
         checkSession('get');
         $_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
         $_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
         if (empty($_GET['source'])) {
             fatal_lang_error('smiley_not_found');
         }
         $smiley = array();
         if (!empty($_GET['after'])) {
             $_GET['after'] = (int) $_GET['after'];
             $smiley = getSmileyPosition($_GET['location'], $_GET['after']);
             if (empty($smiley)) {
                 fatal_lang_error('smiley_not_found');
             }
         } else {
             $smiley['row'] = (int) $_GET['row'];
             $smiley['order'] = -1;
             $smiley['location'] = (int) $_GET['location'];
         }
         moveSmileyPosition($smiley, $_GET['source']);
     }
     $context['smileys'] = getSmileys();
     $context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
     // Make sure all rows are sequential.
     foreach (array_keys($context['smileys']) as $location) {
         $context['smileys'][$location] = array('id' => $location, 'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'], 'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'], 'last_row' => count($context['smileys'][$location]['rows']), 'rows' => array_values($context['smileys'][$location]['rows']));
     }
     // Check & fix smileys that are not ordered properly in the database.
     foreach (array_keys($context['smileys']) as $location) {
         foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row) {
             // Fix empty rows if any.
             if ($id != $smiley_row[0]['row']) {
                 updateSmileyRow($id, $smiley_row[0]['row'], $location);
                 // Only change the first row value of the first smiley (we don't need the others :P).
                 $context['smileys'][$location]['rows'][$id][0]['row'] = $id;
             }
             // Make sure the smiley order is always sequential.
             foreach ($smiley_row as $order_id => $smiley) {
                 if ($order_id != $smiley['order']) {
                     updateSmileyOrder($smiley['id'], $order_id);
                 }
             }
         }
     }
     cache_put_data('parsing_smileys', null, 480);
     cache_put_data('posting_smileys', null, 480);
     createToken('admin-sort');
 }