You are here

class PartyStorageController in Party 8.2

Controller class for users.

This extends the Drupal\Core\Entity\DatabaseStorageController class, adding required special handling for party objects.

Hierarchy

Expanded class hierarchy of PartyStorageController

File

lib/Drupal/party/PartyStorageController.php, line 20
Definition of Drupal\party\PartyStorageController.

Namespace

Drupal\party
View source
class PartyStorageController extends DatabaseStorageController {

  /**
   * Implements EntityAPIControllerInterface.
   *
   * Add in our additional logic for party labels. This is in here rather than
   * PartyStorageController::save() so that we can get the label set as early
   * as possible. The tricky case is when a plugin requires the party to be
   * saved or we fall back to pid which also requires the party to be saved.
   * For those cases, we need to update the label immediately after we write
   * the record but before we invoke modules' implementation of
   * hook_entity_insert(). However, in the more common use case, we ideally
   * want the label to be set before hook_entity_presave() is invoked.
   */
  public function invokeHook($hook, EntityInterface $party) {

    // In presave, trigger our label and primary fields update without a save.
    if ($hook == 'presave') {
      $this
        ->setLabel($party, FALSE);
      $this
        ->setPrimaryFields($party, FALSE);
    }

    // In insert, if our label is empty, set our fallback and save.
    // @TODO: due to the use of the PID party label plugin as a stopper, we
    // we need to compare it to a stopper response from the plugin. Once we
    // rework the label plugins we can get rid of this uglyness and check
    // for an empty.
    if ($hook == 'insert' && $party->label == t('Party')) {
      $party->label = t('Party @pid', array(
        '@pid' => $party->pid,
      ));
      db_query('UPDATE {party} SET label = :label WHERE pid = :pid', array(
        ':label' => $party->label,
        ':pid' => $party
          ->id(),
      ));
    }

    // Trigger the rest of the invoke action.
    parent::invokeHook($hook, $party);
  }

  /**
   * Set the label on a Party object using the first label plugin
   * (when ordered by weight) that returns a non-empty value.
   *
   * This method optionally saves the label to the database as well as
   * generating it, this allows the function to be called independantly of
   * party save. Also, as most Party label plugins require the party to be
   * saved to function correctly, this avoids having to go through the
   * Party::save() method multiple times.
   *
   * @param Party $party
   *  A party object.
   * @param bool $store
   *  Whether we should store the new label.
   */
  public function setLabel($party, $store = TRUE) {
    module_load_include('inc', 'party', 'party.admin');

    // Get the label plugins.
    $label_plugins = party_settings_get_party_label_plugins();

    // Try each plugin, lowest weight first, until a plugin is
    // found that returns a non-empty value.
    foreach ($label_plugins as $path => $label_plugin) {
      $label_callback = $label_plugin['label callback'];
      $label = $label_callback($party);
      if (!empty($label)) {
        $party->label = $label;
        break;
      }
    }

    // No label plugins returned a label, use the party id if possible.
    if (empty($label) && isset($party->id)) {
      $party->label = t('Party @pid', array(
        '@pid' => $party->id,
      ));
    }
    if ($store && isset($party->pid)) {

      // Save the label to the database.
      db_query('UPDATE {party} SET label = :label WHERE pid = :pid', array(
        ':label' => $party->label,
        ':pid' => $party
          ->id(),
      ));
      $this
        ->invokeHook('update', $party);
    }
  }

  /**
   * Set the primary fields for the party.
   *
   * @param $party
   *  A party object.
   */
  public function setPrimaryFields($party, $store = TRUE) {
    $primary_fields = variable_get('party_primary_fields', array());

    // Update the email.
    if (!empty($primary_fields['email'])) {

      // Extract our info from the setting.
      list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email']);
      $controller = drupal_container()
        ->get('plugin.manager.partydata')
        ->createInstance($party);
      if ($entity = $controller
        ->getEntity()) {
        if ($field_name) {
          $items = field_get_items($controller
            ->getDataInfo('entity type'), $entity, $field_name);
          if ($items) {
            $item = reset($items);
            $party->mail = $item[$column];
          }
          else {
            $party->mail = NULL;
          }
        }
        else {
          $party->mail = $entity->{$column};
        }
      }
    }
    if (empty($party->email) && !empty($primary_fields['email2'])) {

      // Extract our info from the setting.
      list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email2']);
      $controller = drupal_container()
        ->get('plugin.manager.partydata')
        ->createInstance($party);
      if ($entity = $controller
        ->getEntity()) {
        if ($field_name) {
          $items = field_get_items($controller
            ->getDataInfo('entity type'), $entity, $field_name);
          if ($items) {
            $item = reset($items);
            $party->mail = $item[$column];
          }
          else {
            $party->mail = NULL;
          }
        }
        else {
          $party->mail = $entity->{$column};
        }
      }
    }
    if ($store && isset($party->pid)) {

      // Save the primary feilds to the database.
      db_query('UPDATE {party} SET mail = :mail WHERE pid = :pid', array(
        ':mail' => $party->mail,
        ':pid' => $party
          ->id(),
      ));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PartyStorageController::invokeHook public function Implements EntityAPIControllerInterface.
PartyStorageController::setLabel public function Set the label on a Party object using the first label plugin (when ordered by weight) that returns a non-empty value.
PartyStorageController::setPrimaryFields public function Set the primary fields for the party.