3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to save a search
20 * Saved Searches are used for saving frequently used queries
21 * regarding the event participations
23 class CRM_Event_Form_Task_SaveSearch
extends CRM_Event_Form_Task
{
26 * Saved search id if any.
33 * Build all the data structures needed to build the form.
37 public function preProcess() {
43 * Build the form object - it consists of
44 * - displaying the QILL (query in local language)
45 * - displaying elements for saving the search
50 public function buildQuickForm() {
51 $this->setTitle(ts('Smart Group'));
53 $query = new CRM_Event_BAO_Query($this->get('formValues'));
54 $qill = $query->qill();
56 // Values from the search form
57 $formValues = $this->controller
->exportValues();
59 // need to save qill for the smarty template
60 $this->assign('qill', $qill);
62 // the name and description are actually stored with the group and not the saved search
63 $this->add('text', 'title', ts('Name'),
64 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
67 $this->addElement('text', 'description', ts('Description'),
68 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
71 // get the group id for the saved search
73 if (isset($this->_id
)) {
74 $params = ['saved_search_id' => $this->_id
];
75 CRM_Contact_BAO_Group
::retrieve($params, $values);
76 $groupId = $values['id'];
78 $this->addDefaultButtons(ts('Update Smart Group'));
81 $this->addDefaultButtons(ts('Save Smart Group'));
82 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
85 $this->addRule('title', ts('Name already exists in Database.'),
86 'objectExists', ['CRM_Contact_DAO_Group', $groupId, 'title']
91 * Process the form after the input has been submitted and validated.
96 public function postProcess() {
97 // saved search form values
98 $formValues = $this->controller
->exportValues();
101 $savedSearch = new CRM_Contact_BAO_SavedSearch();
102 $savedSearch->id
= $this->_id
;
103 $savedSearch->form_values
= serialize($this->get('queryParams'));
104 $savedSearch->save();
105 $this->set('ssID', $savedSearch->id
);
106 CRM_Core_Session
::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Saved'), 'success');
108 // also create a group that is associated with this saved search only if new saved search
110 $params['title'] = $formValues['title'];
111 $params['description'] = $formValues['description'];
112 $params['visibility'] = 'User and User Admin Only';
113 $params['saved_search_id'] = $savedSearch->id
;
114 $params['is_active'] = 1;
117 $params['id'] = CRM_Contact_BAO_SavedSearch
::getName($this->_id
, 'id');
119 $group = CRM_Contact_BAO_Group
::create($params);