mysite_ajax_sort

Definition

mysite_ajax_sort($url = NULL)
mysite.module, line 3293

Description

Process a drag-and-drop sort from jQuery

Parameters

$url The url string passed to the function, which is a JavaScript array in the format: 0[]=123:0[]=456:1[]=789... and generated by mysite.js

Code

function mysite_ajax_sort($url = NULL) {
  // should we execute this code?
  if (!empty($url)) {
    // set the new order as an array
    $order = explode(':', $url);
    $sort = array();
    $array = '';
    $i = 0;
    foreach ($order as $value) {
      $data = explode('[]=', $value);
      $sort[$i]->mid = $data[1];
      $sort[$i]->position = $data[0];
      if ($data[0] != $array) {
        $j = 0;
        $array = $data[0];
      }
      $sort[$i]->sort = $j + 1; // sort starts at one
      $j++;
      $i++;
    }
    // This first MySite item id (mid) will tell us which page this belongs to.
    $mid = $sort[0]->mid;
    $info = db_fetch_object(db_query("SELECT uid, page FROM {mysite_data} WHERE mid = %d", $mid));
    $uid = $info->uid;
    $page = $info->page;
    global $user;
    if (user_access('administer mysite') || $user->uid == $uid) {
     // iterate through the new order and make changes as needed
      $i = 0; // array keys start at 0, but sort keys start at 1.
      foreach ($sort as $key => $value) {
        if (is_numeric($value->mid)) {
          $sql = "UPDATE {mysite_data} SET sort = %d, position = %d WHERE mid = %d";
          db_query($sql, $value->sort, $value->position, $value->mid);
        }
      }
      // clear the user's MySite cache and the defaults, just in case
      $cache_setvariable_get('mysite_cache', 0);
      if ($cache_set > 0) {
        cache_clear_all("mysite:$uid:$page", 'cache');
      }
    }
  }
  exit;
}