b462cc36a9ba23dd950db75be4b01bc51f9c222f
[civicrm-core.git] / CRM / Event / Form / Task / SaveSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class provides the functionality to save a search
22 * Saved Searches are used for saving frequently used queries
23 * regarding the event participations
24 */
25 class CRM_Event_Form_Task_SaveSearch extends CRM_Event_Form_Task {
26
27 /**
28 * Saved search id if any.
29 *
30 * @var int
31 */
32 protected $_id;
33
34 /**
35 * Build all the data structures needed to build the form.
36 *
37 * @return void
38 */
39 public function preProcess() {
40 parent::preProcess();
41 $this->_id = NULL;
42 }
43
44 /**
45 * Build the form object - it consists of
46 * - displaying the QILL (query in local language)
47 * - displaying elements for saving the search
48 *
49 *
50 * @return void
51 */
52 public function buildQuickForm() {
53 CRM_Utils_System::setTitle(ts('Smart Group'));
54 // get the qill
55 $query = new CRM_Event_BAO_Query($this->get('formValues'));
56 $qill = $query->qill();
57
58 // Values from the search form
59 $formValues = $this->controller->exportValues();
60
61 // need to save qill for the smarty template
62 $this->assign('qill', $qill);
63
64 // the name and description are actually stored with the group and not the saved search
65 $this->add('text', 'title', ts('Name'),
66 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
67 );
68
69 $this->addElement('text', 'description', ts('Description'),
70 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
71 );
72
73 // get the group id for the saved search
74 $groupId = NULL;
75 if (isset($this->_id)) {
76 $params = ['saved_search_id' => $this->_id];
77 CRM_Contact_BAO_Group::retrieve($params, $values);
78 $groupId = $values['id'];
79
80 $this->addDefaultButtons(ts('Update Smart Group'));
81 }
82 else {
83 $this->addDefaultButtons(ts('Save Smart Group'));
84 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
85 }
86
87 $this->addRule('title', ts('Name already exists in Database.'),
88 'objectExists', ['CRM_Contact_DAO_Group', $groupId, 'title']
89 );
90 }
91
92 /**
93 * Process the form after the input has been submitted and validated.
94 *
95 *
96 * @return void
97 */
98 public function postProcess() {
99 // saved search form values
100 $formValues = $this->controller->exportValues();
101
102 $session = CRM_Core_Session::singleton();
103
104 //save the search
105 $savedSearch = new CRM_Contact_BAO_SavedSearch();
106 $savedSearch->id = $this->_id;
107 $savedSearch->form_values = serialize($this->get('formValues'));
108 $savedSearch->save();
109 $this->set('ssID', $savedSearch->id);
110 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Saved'), 'success');
111
112 // also create a group that is associated with this saved search only if new saved search
113 $params = [];
114 $params['title'] = $formValues['title'];
115 $params['description'] = $formValues['description'];
116 $params['visibility'] = 'User and User Admin Only';
117 $params['saved_search_id'] = $savedSearch->id;
118 $params['is_active'] = 1;
119
120 if ($this->_id) {
121 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
122 }
123 $group = CRM_Contact_BAO_Group::create($params);
124 }
125
126 }