You are here

function nexcloud::update_accessmetrics in filedepot 7

Same name and namespace in other branches
  1. 6 nexcloud.class.php \nexcloud::update_accessmetrics()
1 call to nexcloud::update_accessmetrics()
filedepotTagCloud::update_accessmetrics in ./nexcloud.class.php
1 method overrides nexcloud::update_accessmetrics()
filedepotTagCloud::update_accessmetrics in ./nexcloud.class.php

File

./nexcloud.class.php, line 254
nexcloud.class.php Tag Cloud class for the filedepot module

Class

nexcloud
@file nexcloud.class.php Tag Cloud class for the filedepot module

Code

function update_accessmetrics($itemid, $tagids = '') {
  if (empty($tagids)) {

    // Retrieve the tags field and convert into an array
    $tags = db_query("SELECT tags FROM {nextag_items} WHERE type=:type AND itemid=:item", array(
      ':type' => $this->_type,
      ':item' => $itemid,
    ))
      ->fetchField();
    if (!empty($tags)) {
      $tagids = explode(',', $tags);
    }
    else {
      $tagids = array();
    }
  }

  // Test that we now have a valid array of tag id's
  if (is_array($tagids) and count($tagids) > 0) {

    // Test that a valid item record exist
    if (db_query("SELECT count(itemid) FROM {nextag_items} WHERE type=:type AND itemid=:item", array(
      ':type' => $this->_type,
      ':item' => $itemid,
    ))
      ->fetchField() > 0) {

      // Get item permissions to determine what rights to use for tag metrics record
      $perms = $this
        ->get_itemperms($itemid, true);

      // For each role or group with view access to this item - create or update the access metric record count.
      $haveGroupsToUpdate = count($perms['groups']) > 0;
      $haveRolesToUpdate = count($perms['roles']) > 0;

      // For each tag word - we need to update (add new or remove any un-used tag metric associated with this permission and tagword
      $metricRecords = array();

      // Will contain the processed metric records
      foreach ($tagids as $id) {
        if (!empty($id)) {
          if ($haveGroupsToUpdate or $haveRolesToUpdate) {

            // An array to handle the logic of whether to process groups, roles, or both
            // Use the key to track the field to update and the value to track the values
            $permAccessMetric = array();
            if ($haveGroupsToUpdate) {
              $permAccessMetric['groupid'] = $perms['groups'];
            }
            if ($haveRolesToUpdate) {
              $permAccessMetric['roleid'] = $perms['roles'];
            }
            foreach ($permAccessMetric as $permKey => $permValue) {
              foreach ($permValue as $permid) {
                if ($permid > 0) {
                  $result = db_query("SELECT id FROM {nextag_metrics} WHERE tagid = :tid AND type = :type AND :permkey = :permid", array(
                    ':tid' => $id,
                    ':type' => $this->_type,
                    ':permkey' => $permKey,
                    'permid' => $permid,
                  ));
                  $numrecs = 0;
                  if ($result) {
                    foreach ($result as $rec) {
                      $numrecs++;
                      if (!in_array($rec->id, $metricRecords)) {
                        $metricRecords[] = $rec->id;
                      }
                    }
                  }
                  if ($numrecs == 0) {
                    $metric = db_query("SELECT metric FROM {nextag_words} WHERE id=:id", array(
                      ':id' => $id,
                    ))
                      ->fetchField();
                    $query = db_insert('nextag_metrics');
                    $query
                      ->fields(array(
                      'tagid',
                      'type',
                      $permKey,
                      'metric',
                      'last_updated',
                    ));
                    $query
                      ->values(array(
                      'tagid' => $id,
                      'type' => $this->_type,
                      $permKey => $permid,
                      'metric' => $metric,
                      'last_updated' => time(),
                    ));
                    $newrecid = $query
                      ->execute();
                    $metricRecords[] = $newrecid;
                  }
                }
              }
            }
          }
        }

        /* Now we should delete any metric records that are not in the processed $metricRecords array
         * They would be records for access permissions that were removed
         */
        if (count($metricRecords) > 0) {
          $recids = implode(',', $metricRecords);
          $query = db_delete('nextag_metrics')
            ->condition('tagid', $id, '=')
            ->condition('id', $metricRecords, 'NOT IN')
            ->execute();
        }
        else {
          db_query("DELETE FROM {nextag_metrics} WHERE tagid = :tid ", array(
            ':tid' => $id,
          ));
        }

        // Delete any tagword records that are no longer used
        $result = db_query("SELECT id FROM {nextag_words} WHERE metric = 0");
        if ($result) {

          // Let's do one more test and make sure no items are using this tagword
          while ($A = $result
            ->fetchAssoc()) {

            /* REGEX - search for id that is the first id or has a leading comma
             *  must then have a trailing , or be the end of the field
             */
            $sql = "SELECT itemid FROM {nextag_items} WHERE type='{$this->_type}' AND ";
            $sql .= "(tags LIKE '{$A['id']},%' OR tags LIKE '%,{$A['id']}' OR tags LIKE '%,{$A['id']},%' OR tags = '{$A['id']}') ";

            //$sql .= "tags REGEXP '(^|,){$A['id']}(,|$)' ";
            if (db_query($sql)
              ->fetchField() == 0) {
              db_query("DELETE FROM {nextag_words} WHERE id = :id", array(
                ':id' => $A['id'],
              ));
            }
          }
        }
      }
    }
  }
}