You are here

function filedepot_views_pre_execute in filedepot 6

Same name and namespace in other branches
  1. 7 views/filedepot.views.inc \filedepot_views_pre_execute()

File

views/filedepot.views.inc, line 124
filedepot.views.inc Contains all Views related API calls and default view creation information

Code

function filedepot_views_pre_execute(&$view) {
  if ($view->name == 'filedepot') {
    $extra_sql = '';

    // Check if user has entered the argument into the view directly -- ie in the view UI
    // If so, we need will use that folder id to remove it and add our own including sub folders.
    if ($view->args[0] > 0) {
      $folder_id = $view->args[0];

      //str_replace("WHERE filedepot_categories.cid = '%s'\n", '', $view->build_info['query']);
      $wherepos = strpos($view->build_info['query'], "WHERE filedepot_categories.cid = '");
      if ($wherepos > 0) {
        $wherepos_end = strpos($view->build_info['query'], "'\n", $wherepos);
        if ($wherepos_end > $wherepos) {
          $view->build_info['query'] = substr($view->build_info['query'], 0, $wherepos) . substr($view->build_info['query'], $wherepos_end + 3);
        }
      }
    }
    else {

      // Check if a folder id is being passed in to the view from a URL argument
      $folder_id = intval(arg(1));
    }
    $filedepot = filedepot_filedepot();

    // if we have a valid folder id determine all viewable subfolders and only generate view for these folders
    if ($folder_id > 0 and $filedepot
      ->checkPermission($folder_id, 'view')) {
      module_load_include('php', 'filedepot', 'lib-common');
      $array_folders = array();
      $array_folders[] = $folder_id;
      filedepot_getRecursiveCatIDs($array_folders, $folder_id, 'view');
      if (!empty($array_folders) and count($array_folders) > 0) {
        $folderlist = implode(',', $array_folders);
        if (strpos($view->build_info['query'], 'WHERE') > 0) {
          $sql_filter_verb = 'AND';
        }
        else {
          $sql_filter_verb = 'WHERE';
        }
        $extra_sql .= " {$sql_filter_verb} filedepot_categories.cid in ({$folderlist})";
      }
    }
    else {
      if (!empty($filedepot->allowableViewFoldersSql)) {
        if (strpos($extra_sql, 'WHERE') > 0 or strpos($view->build_info['query'], 'WHERE') > 0) {
          $sql_filter_verb = 'AND';
        }
        else {
          $sql_filter_verb = 'WHERE';
        }
        if (strpos($view->build_info['query'], 'GROUP BY') > 0) {
          $extra_sql .= " {$sql_filter_verb} filedepot_categories.cid in ({$filedepot->allowableViewFoldersSql}) ";
        }
        else {
          $extra_sql .= " {$sql_filter_verb} filedepot_categories.cid in ({$filedepot->allowableViewFoldersSql}) ";
        }
      }
      else {

        // User does not have any viewable folders
        if (strpos($extra_sql, 'WHERE') > 0 or strpos($view->build_info['query'], 'WHERE') > 0) {
          $sql_filter_verb = 'AND';
        }
        else {
          $sql_filter_verb = 'WHERE';
        }
        $extra_sql .= " {$sql_filter_verb} 1=2 ";
      }
    }
    if (!empty($extra_sql)) {
      if (strpos($view->build_info['query'], 'GROUP BY') > 0) {
        $view->build_info['query'] = str_replace('GROUP BY', " {$extra_sql} GROUP BY", $view->build_info['query']);
      }
      else {
        $view->build_info['query'] .= $extra_sql;
      }
    }
  }
}