You are here

protected function filedepot::__construct in filedepot 7

Same name and namespace in other branches
  1. 6 filedepot.class.php \filedepot::__construct()

File

./filedepot.class.php, line 72
filedepot.class.php Main class for the Filedepot module

Class

filedepot
@file filedepot.class.php Main class for the Filedepot module

Code

protected function __construct() {

  # Singleton Pattern: we don't permit an explicit call of the constructor!
  global $user;
  $this->tmp_storage_path = drupal_realpath('public://') . '/filedepot/';
  $this->tmp_incoming_path = drupal_realpath('public://') . '/filedepot/incoming/';
  $this->root_storage_path = 'private://filedepot/';

  /* @TODO: Need to add logic that will only be executed once to test
   * that the private filesystem has been setup and the filedepot folders
   * for the repository have been created - we can get the $private path.
   */
  $private = variable_get('file_private_path', '');
  $this->recordCountPass1 = variable_get('filedepot_pass1_recordcount', 2);
  $this->recordCountPass2 = variable_get('filedepot_pass2_recordcount', 10);
  $iconsettings = unserialize(variable_get('filedepot_extension_data', ''));
  if (!empty($iconsettings)) {
    $this->iconmap = array_merge($this->iconmap, $iconsettings);
  }
  $permsdata = variable_get('filedepot_default_perms_data', '');
  if (!empty($permsdata)) {
    $permsdata = unserialize($permsdata);
  }
  else {
    $permsdata = array(
      'authenticated user' => array(
        'view',
        'upload',
      ),
    );
  }
  if (isset($permsdata['owner']) and count($permsdata['owner'] > 0)) {
    $this->defOwnerRights = $permsdata['owner'];
  }
  else {
    $this->defOwnerRights = array(
      'view',
      'admin',
    );
  }
  if (isset($permsdata['owner'])) {
    unset($permsdata['owner']);

    // It has now been assigned to defOwnerRights variable
  }
  if (isset($permsdata['group']) and count($permsdata['group'] > 0)) {
    $this->defGroupRights = $permsdata['group'];
  }
  else {
    $this->defGroupRights = array(
      'view',
    );
  }
  if (isset($permsdata['group'])) {
    unset($permsdata['group']);

    // It has now been assigned to defGroupRights variable
  }
  $this->defRoleRights = $permsdata;

  // Remaining permissions are the role assignments
  // Is og enabled?
  if (module_exists('og') and module_exists('og_access')) {
    $this->ogenabled = TRUE;
  }

  //if (user_is_logged_in()) {

  // This cached setting will really only benefit when there are many thousand access records like portal23
  // User setting (all users) is cleared each time a folder permission is updated.
  // But this library is also included for all AJAX requests
  $data = db_query("SELECT allowable_view_folders FROM {filedepot_usersettings} WHERE uid=:uid", array(
    'uid' => $user->uid,
  ))
    ->fetchField();
  if (empty($data)) {
    $this->allowableViewFolders = $this
      ->getAllowableCategories('view', FALSE);
    $data = serialize($this->allowableViewFolders);
    if (db_query("SELECT count(uid) FROM {filedepot_usersettings} WHERE uid=:uid", array(
      'uid' => $user->uid,
    ))
      ->fetchField() == 0) {

      /* Has a problem handling serialized data - we couldn't unserialize the data afterwards.
       * The problem is the pre-constructed SQL statement. When we use the function "udate_sql($sql)",
       * we construct the SQL statement without using any argument. A serialized data normally contains curly brackets.
       * When you call update_sql($sql), it then hands your pre-constructed $sql to the function db_query($sql).
       * Inside the function db_query(), it replace the curly bracket with table prefix blindly,
       * even the curly bracket inside data string are converted.
       * And thus you will not be able to unserialize the data from the table anymore.
       * To get around this, instead of calling update_sql, call db_query($sql, $args).
       * Put all the variables to be inserted into the table into the argument list.
       * This way db_query will only convert the curly bracket surrounding the table name.
       */
      db_query("INSERT INTO {filedepot_usersettings} (uid, allowable_view_folders, notify_newfile, notify_changedfile, allow_broadcasts) VALUES (:uid, :view, :newfile, :changed, :broadcasts)", array(
        ':uid' => $user->uid,
        ':view' => $data,
        ':newfile' => variable_get('filedepot_default_notify_newfile', 0),
        ':changed' => variable_get('filedepot_default_notify_filechange', 0),
        ':broadcasts' => variable_get('filedepot_default_allow_broadcasts', 0),
      ));
    }
    else {
      db_query("UPDATE {filedepot_usersettings} set allowable_view_folders=:view WHERE uid=:uid", array(
        ':view' => $data,
        ':uid' => $user->uid,
      ));
    }
  }
  $this->allowableViewFolders = '';
  if (user_is_logged_in()) {
    if ($this->ogenabled == TRUE) {
      if (variable_get('filedepot_organic_group_mode_enabled', 0) == 1) {
        $this->ogmode_enabled = TRUE;
      }
      if (self::$ogmode_initialized === FALSE) {
        self::$ogmode_initialized = TRUE;

        // Only want to do this once.
        // Using the ctools cache functionality to save which group the user has selected - set in filedepot_main()
        ctools_include('object-cache');
        $gid = ctools_object_cache_get('filedepot', 'grpid');

        // Check if group context was passed into filedepot and if not check if OG was set by another site feature
        if ($gid == 0 and isset($_SESSION['og_last']) and $_SESSION['og_last'] > 0) {
          $gid = $_SESSION['og_last'];
        }
        else {
          if (module_exists('og_context') and isset($_SESSION['og_context']['gid'])) {
            $gid = $_SESSION['og_context']['gid'];
          }
        }
        if ($gid > 0) {
          $this->ogrootfolder = db_query("SELECT cid FROM {filedepot_categories} WHERE group_nid=:gid AND pid=0", array(
            ':gid' => $gid,
          ))
            ->fetchfield();
          if ($this->ogrootfolder !== FALSE and $this->ogrootfolder > 0) {
            $this->allowableViewFolders = array();
            array_push($this->allowableViewFolders, $this->ogrootfolder);
            $folderlist = $this
              ->getRecursiveCatIDs($this->allowableViewFolders, $this->ogrootfolder, 'view');
            $this->allowableGroupViewFoldersSql = implode(',', $folderlist);

            // Format to use for SQL statement - test for allowable categories
          }
        }
      }
    }
  }
  if (empty($this->allowableViewFolders)) {
    $this->allowableViewFolders = unserialize($data);
  }
  $this->allowableViewFoldersSql = implode(',', $this->allowableViewFolders);

  // Format to use for SQL statement - test for allowable categories

  /* } // this is commented out to prevent anonymous users from having to every single time reload their cache, which slows down the process
     else {
     $this->allowableViewFolders    = $this->getAllowableCategories('view', FALSE);
     $this->allowableViewFoldersSql = implode(',', $this->allowableViewFolders); // Format to use for SQL statement - test for allowable categories
     } */
}