| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Mailing_Form_Search extends CRM_Core_Form_Search { |
| 18 | |
| 19 | /** |
| 20 | * Get the default entity being queried. |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public function getDefaultEntity() { |
| 25 | return 'Mailing'; |
| 26 | } |
| 27 | |
| 28 | public function preProcess() { |
| 29 | parent::preProcess(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @throws \CiviCRM_API3_Exception |
| 34 | */ |
| 35 | public function buildQuickForm() { |
| 36 | $parent = $this->controller->getParent(); |
| 37 | $nameTextLabel = ($parent->_sms) ? ts('SMS Name') : ts('Mailing Name'); |
| 38 | |
| 39 | $this->add('text', 'mailing_name', $nameTextLabel, |
| 40 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'title') |
| 41 | ); |
| 42 | |
| 43 | $dateFieldLabel = ($parent->_sms) ? ts('SMS Date') : ts('Mailing Date'); |
| 44 | $this->addDatePickerRange('mailing', $dateFieldLabel); |
| 45 | |
| 46 | $this->add('text', 'sort_name', ts('Created or Sent by'), |
| 47 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name') |
| 48 | ); |
| 49 | |
| 50 | CRM_Mailing_BAO_Query::buildSearchForm($this); |
| 51 | |
| 52 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this); |
| 53 | |
| 54 | // CRM-15434 - Fix mailing search by status in non-English languages |
| 55 | $statusVals = CRM_Core_SelectValues::getMailingJobStatus(); |
| 56 | foreach ($statusVals as $statusId => $statusName) { |
| 57 | $this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName); |
| 58 | } |
| 59 | $this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled')); |
| 60 | |
| 61 | // Search by language, if multi-lingual |
| 62 | $enabledLanguages = CRM_Core_I18n::languages(TRUE); |
| 63 | |
| 64 | if (count($enabledLanguages) > 1) { |
| 65 | $this->addElement('select', 'language', ts('Language'), ['' => ts('- all languages -')] + $enabledLanguages, ['class' => 'crm-select2']); |
| 66 | } |
| 67 | |
| 68 | if ($parent->_sms) { |
| 69 | $this->addElement('hidden', 'sms', $parent->_sms); |
| 70 | } |
| 71 | $this->add('hidden', 'hidden_find_mailings', 1); |
| 72 | |
| 73 | $this->addButtons([ |
| 74 | [ |
| 75 | 'type' => 'refresh', |
| 76 | 'name' => ts('Search'), |
| 77 | 'isDefault' => TRUE, |
| 78 | ], |
| 79 | ]); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return array |
| 84 | * @throws \CRM_Core_Exception |
| 85 | */ |
| 86 | public function setDefaultValues() { |
| 87 | $defaults = $statusVals = []; |
| 88 | $entityDefaults = parent::setDefaultValues(); |
| 89 | $parent = $this->controller->getParent(); |
| 90 | |
| 91 | if ($parent->get('unscheduled')) { |
| 92 | $defaults['status_unscheduled'] = 1; |
| 93 | } |
| 94 | if ($parent->get('scheduled')) { |
| 95 | $statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus()); |
| 96 | $defaults['is_archived'] = 0; |
| 97 | } |
| 98 | if ($parent->get('archived')) { |
| 99 | $defaults['is_archived'] = 1; |
| 100 | } |
| 101 | foreach ($statusVals as $status) { |
| 102 | $defaults['mailing_status'][$status] = 1; |
| 103 | } |
| 104 | |
| 105 | if ($parent->_sms) { |
| 106 | $defaults['sms'] = 1; |
| 107 | } |
| 108 | return array_merge($defaults, $entityDefaults); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @throws \CRM_Core_Exception |
| 113 | */ |
| 114 | public function postProcess() { |
| 115 | $this->setFormValues(); |
| 116 | $params = $this->_formValues; |
| 117 | |
| 118 | if (!empty($params['mailing_relative'])) { |
| 119 | list($params['mailing_low'], $params['mailing_high']) = CRM_Utils_Date::getFromTo($params['mailing_relative'], $params['mailing_low'], $params['mailing_high']); |
| 120 | unset($params['mailing_relative']); |
| 121 | } |
| 122 | elseif (!empty($params['mailing_high'])) { |
| 123 | $params['mailing_high'] .= ' ' . '23:59:59'; |
| 124 | } |
| 125 | |
| 126 | $parent = $this->controller->getParent(); |
| 127 | if (!empty($params)) { |
| 128 | $fields = [ |
| 129 | 'mailing_name', |
| 130 | 'mailing_low', |
| 131 | 'mailing_high', |
| 132 | 'sort_name', |
| 133 | 'campaign_id', |
| 134 | 'mailing_status', |
| 135 | 'sms', |
| 136 | 'status_unscheduled', |
| 137 | 'is_archived', |
| 138 | 'language', |
| 139 | 'hidden_find_mailings', |
| 140 | ]; |
| 141 | foreach ($fields as $field) { |
| 142 | if (isset($params[$field]) && |
| 143 | !CRM_Utils_System::isNull($params[$field]) |
| 144 | ) { |
| 145 | $parent->set($field, $params[$field]); |
| 146 | } |
| 147 | else { |
| 148 | $parent->set($field, NULL); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Handle force=1 in the url. |
| 156 | * |
| 157 | * Search field metadata is normally added in buildForm but we are bypassing that in this flow |
| 158 | * (I've always found the flow kinda confusing & perhaps that is the problem but this mitigates) |
| 159 | * |
| 160 | * @throws \CiviCRM_API3_Exception |
| 161 | */ |
| 162 | protected function handleForcedSearch() { |
| 163 | $this->setSearchMetadata(); |
| 164 | $this->addContactSearchFields(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set the metadata for the form. |
| 169 | * |
| 170 | * @throws \CiviCRM_API3_Exception |
| 171 | */ |
| 172 | protected function setSearchMetadata() { |
| 173 | $this->addSearchFieldMetadata(['Mailing' => CRM_Mailing_BAO_Query::getSearchFieldMetadata()]); |
| 174 | } |
| 175 | |
| 176 | } |