Merge pull request #4961 from pratikshad/core-bugs
[civicrm-core.git] / CRM / Contact / Form / Search / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class 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
c7b8b4e4
DL
48 list(
49 $this->_customSearchID,
6a488035
TO
50 $this->_customSearchClass,
51 $formValues
353ffa53 52 ) = CRM_Contact_BAO_SearchCustom::details($csID, $ssID, $gID);
6a488035
TO
53
54 if (!$this->_customSearchID) {
55 CRM_Core_Error::fatal('Could not get details for custom search.');
56 }
57
c7b8b4e4
DL
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
6a488035
TO
62 if (!empty($formValues)) {
63 $this->_formValues = $formValues;
64 }
65
66 // set breadcrumb to return to Custom Search listings page
353ffa53
TO
67 $breadCrumb = array(
68 array(
6ea503d4 69 'title' => ts('Custom Searches'),
6a488035
TO
70 'url' => CRM_Utils_System::url('civicrm/contact/search/custom/list',
71 'reset=1'
72 ),
7c550ca0 73 ),
353ffa53 74 );
6a488035
TO
75 CRM_Utils_System::appendBreadCrumb($breadCrumb);
76
77 // use the custom selector
78 self::$_selectorName = 'CRM_Contact_Selector_Custom';
79
80 $this->set('customSearchID', $this->_customSearchID);
81 $this->set('customSearchClass', $this->_customSearchClass);
82
83 parent::preProcess();
84
85 // instantiate the new class
481a74f4 86 $this->_customClass = new $this->_customSearchClass($this->_formValues);
4e54c348
PJ
87
88 // CRM-12747
89 if (isset($this->_customClass->_permissionedComponent) &&
353ffa53
TO
90 !self::isPermissioned($this->_customClass->_permissionedComponent)
91 ) {
4e54c348
PJ
92 CRM_Utils_System::permissionDenied();
93 }
6a488035
TO
94 }
95
86538308
EM
96 /**
97 * This virtual function is used to set the default values of
98 * various form elements
99 *
100 * access public
101 *
a6c01b45
CW
102 * @return array
103 * reference to the array of default values
86538308
EM
104 */
105 /**
106 * @return array
107 */
00be9182 108 public function setDefaultValues() {
6a488035
TO
109 if (method_exists($this->_customSearchClass, 'setDefaultValues')) {
110 return $this->_customClass->setDefaultValues();
111 }
112 return $this->_formValues;
113 }
114
00be9182 115 public function buildQuickForm() {
6a488035
TO
116 $this->_customClass->buildForm($this);
117
118 parent::buildQuickForm();
119 }
120
86538308
EM
121 /**
122 * Use the form name to create the tpl file name
123 *
124 * @return string
86538308
EM
125 */
126 /**
127 * @return string
128 */
00be9182 129 public function getTemplateFileName() {
6a488035
TO
130
131 $ext = CRM_Extension_System::singleton()->getMapper();
132
133 if ($ext->isExtensionClass(CRM_Utils_System::getClassName($this->_customClass))) {
134 $fileName = $ext->getTemplatePath(CRM_Utils_System::getClassName($this->_customClass)) . '/' . $ext->getTemplateName(CRM_Utils_System::getClassName($this->_customClass));
135 }
136 else {
137 $fileName = $this->_customClass->templateFile();
138 }
139
140 return $fileName ? $fileName : parent::getTemplateFileName();
141 }
142
00be9182 143 public function postProcess() {
6a488035
TO
144 $this->set('isAdvanced', '3');
145 $this->set('isCustom', '1');
146
147 // get user submitted values
148 // get it from controller only if form has been submitted, else preProcess has set this
149 if (!empty($_POST)) {
150 $this->_formValues = $this->controller->exportValues($this->_name);
151
152 $this->_formValues['customSearchID'] = $this->_customSearchID;
153 $this->_formValues['customSearchClass'] = $this->_customSearchClass;
154 }
155
156 //use the custom selector
157 self::$_selectorName = 'CRM_Contact_Selector_Custom';
158
159 parent::postProcess();
160 }
161
86538308
EM
162 /**
163 * Return a descriptive name for the page, used in wizard header
164 *
165 * @return string
86538308
EM
166 */
167 /**
168 * @return string
169 */
6a488035
TO
170 public function getTitle() {
171 return ts('Custom Search');
172 }
6a488035 173
86538308
EM
174 /**
175 * @param $components
176 *
177 * @return bool
178 */
00be9182 179 public function isPermissioned($components) {
4e54c348
PJ
180 if (empty($components)) {
181 return TRUE;
182 }
183 if (is_array($components)) {
184 foreach ($components as $component) {
185 if (!CRM_Core_Permission::access($component)) {
186 return FALSE;
187 }
188 }
189 }
190 else {
191 if (!CRM_Core_Permission::access($components)) {
192 return FALSE;
193 }
194 }
195 return TRUE;
196 }
232624b1 197}