You are here

class SearchApiSavedSearch in Search API Saved Searches 7

Class representing "Saved searches" settings.

Hierarchy

Expanded class hierarchy of SearchApiSavedSearch

1 string reference to 'SearchApiSavedSearch'
search_api_saved_searches_entity_info in ./search_api_saved_searches.module
Implements hook_entity_info().

File

./search_api_saved_searches.search_entity.inc, line 11
Contains the entity class for saved searches.

View source
class SearchApiSavedSearch extends Entity {

  /**
   * The user that owns this saved search.
   *
   * @var stdClass
   */
  protected $user;

  /**
   * The settings this saved search uses.
   *
   * @var SearchApiSavedSearchesSettings
   */
  protected $settings;

  /**
   * The search index this saved search uses.
   *
   * @var SearchApiIndex
   */
  protected $index;

  // Database values that will be set when object is loaded

  /**
   * @var integer
   */
  public $id;

  /**
   * @var integer
   */
  public $uid;

  /**
   * @var string
   */
  public $settings_id;

  /**
   * @var boolean
   */
  public $enabled;

  /**
   * @var string
   */
  public $name;

  /**
   * @var string
   */
  public $mail;

  /**
   * @var integer
   */
  public $created;

  /**
   * @var integer
   */
  public $last_queued;

  /**
   * @var integer
   */
  public $last_execute;

  /**
   * @var integer
   */
  public $notify_interval;

  /**
   * Array representing the search query to execute, containing:
   *   keys: The parsed fulltext keys.
   *   fields: The fields that will be fulltext-searched.
   *   filters: An array of filters, as used in SearchApiQueryFilterInterface.
   *   options: The query options.
   *
   * @var array
   */
  public $query;

  /**
   * @var array
   */
  public $options;

  /**
   * @var string
   */
  public $results;

  /**
   * Constructor as a helper to the parent constructor.
   */
  public function __construct(array $values = array()) {
    parent::__construct($values, 'search_api_saved_search');
  }

  /**
   * Permanently saves the entity.
   *
   * @see entity_save()
   */
  public function save() {
    $settings = $this
      ->settings();
    if (!$this->enabled && empty($this->options['key']) || !empty($settings->options['registered_user_delete_key'])) {
      $this->options['key'] = drupal_hash_base64(drupal_random_bytes(12));
    }
    $date_field = isset($settings->options['date_field']) ? $settings->options['date_field'] : NULL;
    if ($this->enabled && !isset($this->results) && !$date_field) {
      $results = array();
      $response = $this
        ->query()
        ->execute();
      $this->results = implode(',', array_keys($response['results']));
    }
    $ret = parent::save();
    if ($ret == SAVED_NEW && !$this->enabled) {
      $params = array(
        'user' => user_load($this->uid),
        'search' => $this,
      );
      drupal_mail('search_api_saved_searches', 'activate', $this->mail, user_preferred_language($params['user']), $params);
    }
    return $ret;
  }

  /**
   * @return
   *   The user that owns this saved search.
   */
  public function user() {
    if (!isset($this->user)) {
      $this->user = user_load($this->uid);
    }
    return $this->user;
  }

  /**
   * @return SearchApiSavedSearchesSettings
   *   The settings this saved search uses.
   *
   * @throws SearchApiException
   *   If the settings don't exist.
   */
  public function settings() {
    if (!isset($this->settings)) {
      $this->settings = search_api_saved_searches_settings_load($this->settings_id);
    }
    if (!$this->settings) {
      throw new SearchApiException(t("The saved search settings with the ID %id don't exist, but are used by an existing saved search.", array(
        '%id' => $this->settings_id,
      )));
    }
    return $this->settings;
  }

  /**
   * @return SearchApiIndex
   *   The index this saved search uses.
   *
   * @throws SearchApiException
   *   If the index doesn't exist.
   */
  public function index() {
    if (!isset($this->index)) {
      $this->index = search_api_index_load($this
        ->settings()->index_id);
    }
    if (!$this->index) {
      throw new SearchApiException(t("The index with the ID %id doesn't exist, but has saved search settings attached.", array(
        '%id' => $this
          ->settings()->index_id,
      )));
    }
    return $this->index;
  }

  /**
   * @return SearchApiQueryInterface
   *   A query for getting all new results for this saved search.
   *
   * @throws SearchApiException
   *   If the saved search's index is disabled.
   */
  public function query() {
    $index = $this
      ->index();
    $query = $index
      ->query($this->query['options']);
    if ($this->query['keys']) {
      $query
        ->keys($this->query['keys']);
    }
    if ($this->query['fields']) {
      $fields = (array) $this->query['fields'];
      $fields = array_intersect($fields, $index
        ->getFulltextFields());
      if ($fields) {
        $query
          ->fields($fields);
      }
    }
    if ($this->query['filters']) {
      $filters =& $query
        ->getFilter()
        ->getFilters();
      $filters = $this->query['filters'];
    }
    return $query;
  }

  /**
   * Return the URL where this search can be viewed, if any.
   */
  public function url() {
    if (isset($this->options['page']['path'])) {
      return url($this->options['page']['path'], $this->options['page']);
    }
  }

  /**
   * Return a link to the URL where this search can be viewed, if any.
   */
  public function l($text) {
    if (isset($this->options['page']['path'])) {
      return l($text, $this->options['page']['path'], $this->options['page']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
SearchApiSavedSearch::$created public property
SearchApiSavedSearch::$enabled public property
SearchApiSavedSearch::$id public property
SearchApiSavedSearch::$index protected property The search index this saved search uses.
SearchApiSavedSearch::$last_execute public property
SearchApiSavedSearch::$last_queued public property
SearchApiSavedSearch::$mail public property
SearchApiSavedSearch::$name public property
SearchApiSavedSearch::$notify_interval public property
SearchApiSavedSearch::$options public property
SearchApiSavedSearch::$query public property Array representing the search query to execute, containing: keys: The parsed fulltext keys. fields: The fields that will be fulltext-searched. filters: An array of filters, as used in SearchApiQueryFilterInterface. options: The query options.
SearchApiSavedSearch::$results public property
SearchApiSavedSearch::$settings protected property The settings this saved search uses.
SearchApiSavedSearch::$settings_id public property
SearchApiSavedSearch::$uid public property
SearchApiSavedSearch::$user protected property The user that owns this saved search.
SearchApiSavedSearch::index public function
SearchApiSavedSearch::l public function Return a link to the URL where this search can be viewed, if any.
SearchApiSavedSearch::query public function
SearchApiSavedSearch::save public function Permanently saves the entity. Overrides Entity::save
SearchApiSavedSearch::settings public function
SearchApiSavedSearch::url public function Return the URL where this search can be viewed, if any.
SearchApiSavedSearch::user public function
SearchApiSavedSearch::__construct public function Constructor as a helper to the parent constructor. Overrides Entity::__construct