Merge pull request #12647 from compucorp/fix-case-type-issues
[civicrm-core.git] / CRM / Contact / Form / Search / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33 class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search {
34
35 protected $_customClass = NULL;
36
37 public function preProcess() {
38 $this->set('searchFormName', 'Custom');
39
40 $this->set('context', 'custom');
41
42 $csID = CRM_Utils_Request::retrieve('csid', 'Integer', $this);
43 $ssID = CRM_Utils_Request::retrieve('ssID', 'Integer', $this);
44 $gID = CRM_Utils_Request::retrieve('gid', 'Integer', $this);
45
46 list(
47 $this->_customSearchID,
48 $this->_customSearchClass,
49 $formValues
50 ) = CRM_Contact_BAO_SearchCustom::details($csID, $ssID, $gID);
51
52 if (!$this->_customSearchID) {
53 CRM_Core_Error::fatal('Could not get details for custom search.');
54 }
55
56 // stash this as a hidden element so we can potentially go there if the session
57 // is reset but this is available in the POST
58 $this->addElement('hidden', 'csid', $csID);
59
60 if (!empty($formValues)) {
61 $this->_formValues = $formValues;
62 }
63
64 // set breadcrumb to return to Custom Search listings page
65 $breadCrumb = array(
66 array(
67 'title' => ts('Custom Searches'),
68 'url' => CRM_Utils_System::url('civicrm/contact/search/custom/list',
69 'reset=1'
70 ),
71 ),
72 );
73 CRM_Utils_System::appendBreadCrumb($breadCrumb);
74
75 // use the custom selector
76 self::$_selectorName = 'CRM_Contact_Selector_Custom';
77
78 $this->set('customSearchID', $this->_customSearchID);
79 $this->set('customSearchClass', $this->_customSearchClass);
80
81 parent::preProcess();
82
83 // instantiate the new class
84 $this->_customClass = new $this->_customSearchClass($this->_formValues);
85
86 $this->addFormRule(array($this->_customClass, 'formRule'), $this);
87
88 // CRM-12747
89 if (isset($this->_customClass->_permissionedComponent) &&
90 !self::isPermissioned($this->_customClass->_permissionedComponent)
91 ) {
92 CRM_Utils_System::permissionDenied();
93 }
94 }
95
96 /**
97 * Add local and global form rules.
98 */
99 public function addRules() {
100 $this->addFormRule(array($this->_customClass, 'formRule'));
101 }
102
103 /**
104 * Set the default values of various form elements.
105 *
106 * @return array
107 * reference to the array of default values
108 */
109 public function setDefaultValues() {
110 if (method_exists($this->_customSearchClass, 'setDefaultValues')) {
111 return $this->_customClass->setDefaultValues();
112 }
113 return $this->_formValues;
114 }
115
116 /**
117 * Builds the list of tasks or actions that a searcher can perform on a result set.
118 *
119 * @return array
120 */
121 public function buildTaskList() {
122 // call the parent method to populate $this->_taskList for the custom search
123 parent::buildTaskList();
124
125 return $this->_customClass->buildTaskList($this);
126 }
127
128 public function buildQuickForm() {
129 $this->_customClass->buildForm($this);
130
131 parent::buildQuickForm();
132 }
133
134 /**
135 * Use the form name to create the tpl file name.
136 *
137 * @return string
138 */
139 /**
140 * @return string
141 */
142 public function getTemplateFileName() {
143
144 $ext = CRM_Extension_System::singleton()->getMapper();
145
146 if ($ext->isExtensionClass(CRM_Utils_System::getClassName($this->_customClass))) {
147 $fileName = $ext->getTemplatePath(CRM_Utils_System::getClassName($this->_customClass)) . '/' . $ext->getTemplateName(CRM_Utils_System::getClassName($this->_customClass));
148 }
149 else {
150 $fileName = $this->_customClass->templateFile();
151 }
152
153 return $fileName ? $fileName : parent::getTemplateFileName();
154 }
155
156 public function postProcess() {
157 $this->set('isAdvanced', '3');
158 $this->set('isCustom', '1');
159
160 // get user submitted values
161 // get it from controller only if form has been submitted, else preProcess has set this
162 if (!empty($_POST)) {
163 $this->_formValues = $this->controller->exportValues($this->_name);
164
165 $this->_formValues['customSearchID'] = $this->_customSearchID;
166 $this->_formValues['customSearchClass'] = $this->_customSearchClass;
167 }
168
169 //use the custom selector
170 self::$_selectorName = 'CRM_Contact_Selector_Custom';
171
172 parent::postProcess();
173 }
174
175 /**
176 * Return a descriptive name for the page, used in wizard header.
177 *
178 * @return string
179 */
180 public function getTitle() {
181 return ts('Custom Search');
182 }
183
184 /**
185 * @param $components
186 *
187 * @return bool
188 */
189 public function isPermissioned($components) {
190 if (empty($components)) {
191 return TRUE;
192 }
193 if (is_array($components)) {
194 foreach ($components as $component) {
195 if (!CRM_Core_Permission::access($component)) {
196 return FALSE;
197 }
198 }
199 }
200 else {
201 if (!CRM_Core_Permission::access($components)) {
202 return FALSE;
203 }
204 }
205 return TRUE;
206 }
207
208 }