You are here

function photos_access_update_access in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 photos_access/photos_access.module \photos_access_update_access()
  2. 7.3 photos_access/photos_access.module \photos_access_update_access()
  3. 6.0.x photos_access/photos_access.module \photos_access_update_access()

Update access to album.

2 calls to photos_access_update_access()
photos_access_node_insert in photos_access/photos_access.module
Implements hook_ENTITY_TYPE_insert().
photos_access_node_update in photos_access/photos_access.module
Implements hook_ENTITY_TYPE_update().

File

photos_access/photos_access.module, line 447
Implementation of photos_access.module.

Code

function photos_access_update_access(NodeInterface $node, $privacy_settings) {

  // @todo cleanup and simplify with access_id.
  if (\Drupal::config('photos.settings')
    ->get('photos_access_' . $node
    ->getType())) {
    if (!$privacy_settings['eid']) {
      if ($privacy_settings['updateuser']) {

        // Check if row already exists for this node.
        $db = \Drupal::database();
        $acc['updateid'] = $db
          ->query("SELECT id FROM {photos_access_album} WHERE nid = :nid", [
          ':nid' => $node
            ->id(),
        ])
          ->fetchField();
        $privacy_settings['vid'] = $privacy_settings['eid'] = $acc['updateid'];
        $db = \Drupal::database();
        if ($acc['updateid']) {

          // Update existing record.
          $db
            ->update('photos_access_album')
            ->fields([
            'viewid' => $privacy_settings['viewid'],
          ])
            ->condition('id', $acc['updateid'])
            ->execute();
        }
        else {

          // Enter new record.
          $acc['updateid'] = $db
            ->insert('photos_access_album')
            ->fields([
            'nid' => $node
              ->id(),
            'viewid' => $privacy_settings['viewid'],
          ])
            ->execute();
        }
        _photos_access_usersave($privacy_settings['updateuser'], $acc['updateid']);
      }
    }
    else {

      // Remove collaborators.
      if (isset($privacy_settings['updateremove']) && !empty($privacy_settings['updateremove'])) {
        _photos_access_usersdel($privacy_settings['updateremove'], $privacy_settings['eid']);
      }

      // Save collaborators.
      if (isset($privacy_settings['updateuser']) && !empty($privacy_settings['updateuser'])) {
        _photos_access_usersave($privacy_settings['updateuser'], $privacy_settings['eid']);
      }
      $acc['updateid'] = $privacy_settings['eid'];
    }
    if (!$privacy_settings['vid']) {

      // Double check for existing photos_access_album record.
      $db = \Drupal::database();
      $privacy_settings['vid'] = $privacy_settings['eid'] = $db
        ->query("SELECT id FROM {photos_access_album} WHERE nid = :nid", [
        ':nid' => $node
          ->id(),
      ])
        ->fetchField();
    }
    if (!$privacy_settings['vid']) {

      // Insert new record.
      $db = \Drupal::database();
      $acc['viewid'] = $db
        ->insert('photos_access_album')
        ->fields([
        'nid' => $node
          ->id(),
        'viewid' => isset($privacy_settings['viewid']) ? $privacy_settings['viewid'] : 0,
        'pass' => isset($privacy_settings['pass']) && !empty($privacy_settings['pass']) ? md5($privacy_settings['pass']) : 0,
      ])
        ->execute();
      if ($privacy_settings['viewid'] && $privacy_settings['viewuser']) {
        _photos_access_usersave($privacy_settings['viewuser'], $acc['viewid'], FALSE);
      }
    }
    else {

      // Update existing record.
      switch ($privacy_settings['viewid']) {
        case 0:
        case 1:
          $db = \Drupal::database();
          $db
            ->update('photos_access_album')
            ->fields([
            ':viewid' => $privacy_settings['viewid'],
          ])
            ->condition('id', $privacy_settings['vid'])
            ->execute();

          // Delete designated users.
          _photos_access_usersdel(0, $privacy_settings['vid'], 1);
          break;
        case 2:
          $db = \Drupal::database();
          $db
            ->update('photos_access_album')
            ->fields([
            ':viewid' => $privacy_settings['viewid'],
          ])
            ->condition('id', $privacy_settings['vid'])
            ->execute();
          if ($privacy_settings['viewuser']) {
            _photos_access_usersave($privacy_settings['viewuser'], $privacy_settings['vid'], FALSE);
          }
          if (isset($privacy_settings['viewremove'])) {
            _photos_access_usersdel($privacy_settings['viewremove'], $privacy_settings['vid']);
          }
          break;
        case 3:

          // @todo add option to integrate aes module and encrypt passwords with that.
          $db = \Drupal::database();
          $old_pass = $db
            ->query("SELECT pass FROM {photos_access_album} WHERE id = :id", [
            ':id' => $privacy_settings['vid'],
          ])
            ->fetchField();
          $pass = isset($privacy_settings['pass']) && !empty($privacy_settings['pass']) ? md5($privacy_settings['pass']) : $old_pass;

          // Update password.
          $db = \Drupal::database();
          $query = $db
            ->update('photos_access_album');
          $update_fields = [
            'viewid' => $privacy_settings['viewid'],
          ];
          if (!empty($pass) && $pass != $old_pass) {
            $update_fields['pass'] = $pass;
          }
          $query
            ->fields($update_fields);
          $query
            ->condition('id', $privacy_settings['vid']);
          $query
            ->execute();

          // Delete designated users.
          _photos_access_usersdel(0, $privacy_settings['vid'], 1);
      }
      $acc['viewid'] = $privacy_settings['viewid'];
      $acc['vid'] = $privacy_settings['vid'];
    }
  }
}