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