search_api_saved_searches.settings_entity.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity class for stored "Saved searches" settings of search
  5. * indexes.
  6. */
  7. /**
  8. * Class representing "Saved searches" settings.
  9. */
  10. class SearchApiSavedSearchesSettings extends Entity {
  11. /**
  12. * The search index these settings are for.
  13. *
  14. * @var SearchApiIndex
  15. */
  16. protected $index;
  17. // Database values that will be set when object is loaded
  18. /**
  19. * @var integer
  20. */
  21. public $id;
  22. /**
  23. * @var string
  24. */
  25. public $delta;
  26. /**
  27. * @var string
  28. */
  29. public $index_id;
  30. /**
  31. * @var boolean
  32. */
  33. public $enabled;
  34. /**
  35. * @var array
  36. */
  37. public $options;
  38. /**
  39. * Constructor as a helper to the parent constructor.
  40. */
  41. public function __construct(array $values = array()) {
  42. parent::__construct($values, 'search_api_saved_searches_settings');
  43. }
  44. /**
  45. * @return SearchApiIndex
  46. * The index these saved search settings are for.
  47. *
  48. * @throws SearchApiException
  49. * If the index doesn't exist.
  50. */
  51. public function index() {
  52. if (!isset($this->index)) {
  53. $this->index = search_api_index_load($this->index_id);
  54. }
  55. if (!$this->index) {
  56. throw new SearchApiException(t("The index with the ID %id doesn't exist, but has saved search settings attached.", array('%id' => $this->index_id)));
  57. }
  58. return $this->index;
  59. }
  60. /**
  61. * Gets the translated value of an option via i18n string translations.
  62. *
  63. * @param $property
  64. * The name of the property stored in the options, as declared for i18n;
  65. * e.g. "mail.notify.title".
  66. * @param $langcode
  67. * (optional) The language code of the language to which the value should
  68. * be translated. If set to NULL, the default display language is being
  69. * used.
  70. *
  71. * @return
  72. * The raw, translated property value; or the raw, un-translated value if no
  73. * translation is available.
  74. *
  75. * @see SearchApiSavedSearchesSettingsI18nController
  76. */
  77. public function getTranslatedOption($property, $langcode = NULL) {
  78. $value = drupal_array_get_nested_value($this->options, explode('.', $property));
  79. if (isset($value) && module_exists('search_api_saved_searches_i18n') && function_exists('i18n_string')) {
  80. $name = 'search_api_saved_searches:search_api_saved_searches_settings:' . $this->identifier() . ':' . $property;
  81. if (is_array($value)) {
  82. // Handle arrays of values, i.e. interval_options.
  83. foreach ($value as $key => $data) {
  84. $value[$key] = i18n_string($name . ".$key", $data, array('langcode' => $langcode, 'sanitize' => FALSE));
  85. }
  86. return $value;
  87. }
  88. else {
  89. return i18n_string($name, $value, array('langcode' => $langcode, 'sanitize' => FALSE));
  90. }
  91. }
  92. return $value;
  93. }
  94. }