| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | class CRM_Contact_Form_Search_Custom_Basic extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { |
| 34 | |
| 35 | protected $_query; |
| 36 | protected $_aclFrom = NULL; |
| 37 | protected $_aclWhere = NULL; |
| 38 | |
| 39 | /** |
| 40 | * Class constructor. |
| 41 | * |
| 42 | * @param array $formValues |
| 43 | */ |
| 44 | public function __construct(&$formValues) { |
| 45 | parent::__construct($formValues); |
| 46 | |
| 47 | $this->normalize(); |
| 48 | $this->_columns = array( |
| 49 | '' => 'contact_type', |
| 50 | ts('Name') => 'sort_name', |
| 51 | ts('Address') => 'street_address', |
| 52 | ts('City') => 'city', |
| 53 | ts('State') => 'state_province', |
| 54 | ts('Postal') => 'postal_code', |
| 55 | ts('Country') => 'country', |
| 56 | ts('Email') => 'email', |
| 57 | ts('Phone') => 'phone', |
| 58 | ); |
| 59 | |
| 60 | $params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 61 | $returnProperties = array(); |
| 62 | $returnProperties['contact_sub_type'] = 1; |
| 63 | |
| 64 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 65 | 'address_options', TRUE, NULL, TRUE |
| 66 | ); |
| 67 | |
| 68 | foreach ($this->_columns as $name => $field) { |
| 69 | if (in_array($field, array( |
| 70 | 'street_address', |
| 71 | 'city', |
| 72 | 'state_province', |
| 73 | 'postal_code', |
| 74 | 'country', |
| 75 | )) && empty($addressOptions[$field]) |
| 76 | ) { |
| 77 | unset($this->_columns[$name]); |
| 78 | continue; |
| 79 | } |
| 80 | $returnProperties[$field] = 1; |
| 81 | } |
| 82 | |
| 83 | $this->_query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL, |
| 84 | FALSE, FALSE, 1, FALSE, FALSE |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Normalize the form values to make it look similar to the advanced form values. |
| 90 | * |
| 91 | * This prevents a ton of work downstream and allows us to use the same code for |
| 92 | * multiple purposes (queries, save/edit etc) |
| 93 | */ |
| 94 | public function normalize() { |
| 95 | $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues); |
| 96 | if ($contactType && !is_array($contactType)) { |
| 97 | unset($this->_formValues['contact_type']); |
| 98 | $this->_formValues['contact_type'][$contactType] = 1; |
| 99 | } |
| 100 | |
| 101 | $group = CRM_Utils_Array::value('group', $this->_formValues); |
| 102 | if ($group && !is_array($group)) { |
| 103 | unset($this->_formValues['group']); |
| 104 | $this->_formValues['group'][$group] = 1; |
| 105 | } |
| 106 | |
| 107 | $tag = CRM_Utils_Array::value('tag', $this->_formValues); |
| 108 | if ($tag && !is_array($tag)) { |
| 109 | unset($this->_formValues['tag']); |
| 110 | $this->_formValues['tag'][$tag] = 1; |
| 111 | } |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param CRM_Core_Form $form |
| 118 | */ |
| 119 | public function buildForm(&$form) { |
| 120 | //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form |
| 121 | // this is loaded onto then replace with something like '__' & test |
| 122 | $separator = CRM_Core_DAO::VALUE_SEPARATOR; |
| 123 | $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator); |
| 124 | $form->add('select', 'contact_type', ts('Find...'), $contactTypes, FALSE, array('class' => 'crm-select2 huge')); |
| 125 | |
| 126 | // add select for groups |
| 127 | $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup(); |
| 128 | $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge')); |
| 129 | |
| 130 | // add select for categories |
| 131 | $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
| 132 | $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge')); |
| 133 | |
| 134 | // text for sort_name |
| 135 | $form->add('text', 'sort_name', ts('Name')); |
| 136 | |
| 137 | $form->assign('elements', array('sort_name', 'contact_type', 'group', 'tag')); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @return CRM_Contact_DAO_Contact |
| 142 | */ |
| 143 | public function count() { |
| 144 | return $this->_query->searchQuery(0, 0, NULL, TRUE); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param int $offset |
| 149 | * @param int $rowCount |
| 150 | * @param null $sort |
| 151 | * @param bool $includeContactIDs |
| 152 | * @param bool $justIDs |
| 153 | * |
| 154 | * @return CRM_Contact_DAO_Contact |
| 155 | */ |
| 156 | public function all( |
| 157 | $offset = 0, |
| 158 | $rowCount = 0, |
| 159 | $sort = NULL, |
| 160 | $includeContactIDs = FALSE, |
| 161 | $justIDs = FALSE |
| 162 | ) { |
| 163 | return $this->_query->searchQuery( |
| 164 | $offset, |
| 165 | $rowCount, |
| 166 | $sort, |
| 167 | FALSE, |
| 168 | $includeContactIDs, |
| 169 | FALSE, |
| 170 | $justIDs, |
| 171 | TRUE |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @return string |
| 177 | */ |
| 178 | public function from() { |
| 179 | $this->buildACLClause('contact_a'); |
| 180 | $from = $this->_query->_fromClause; |
| 181 | $from .= "{$this->_aclFrom}"; |
| 182 | return $from; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param bool $includeContactIDs |
| 187 | * |
| 188 | * @return string|void |
| 189 | */ |
| 190 | public function where($includeContactIDs = FALSE) { |
| 191 | if ($whereClause = $this->_query->whereClause()) { |
| 192 | if ($this->_aclWhere) { |
| 193 | $whereClause .= " AND {$this->_aclWhere}"; |
| 194 | } |
| 195 | return $whereClause; |
| 196 | } |
| 197 | return ' (1) '; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @return string |
| 202 | */ |
| 203 | public function templateFile() { |
| 204 | return 'CRM/Contact/Form/Search/Basic.tpl'; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @return CRM_Contact_BAO_Query |
| 209 | */ |
| 210 | public function getQueryObj() { |
| 211 | return $this->_query; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @param string $tableAlias |
| 216 | */ |
| 217 | public function buildACLClause($tableAlias = 'contact') { |
| 218 | list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias); |
| 219 | } |
| 220 | |
| 221 | } |