Merge pull request #16161 from civicrm/5.21
[civicrm-core.git] / CRM / Event / Form / Task / SaveSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
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 */
25class CRM_Event_Form_Task_SaveSearch extends CRM_Event_Form_Task {
26
27 /**
66f9e52b 28 * Saved search id if any.
6a488035
TO
29 *
30 * @var int
31 */
32 protected $_id;
33
34 /**
66f9e52b 35 * Build all the data structures needed to build the form.
6a488035
TO
36 *
37 * @return void
95ea96be 38 */
79d7553f 39 public function preProcess() {
6a488035
TO
40 parent::preProcess();
41 $this->_id = NULL;
42 }
43
44 /**
c490a46a 45 * Build the form object - it consists of
6a488035
TO
46 * - displaying the QILL (query in local language)
47 * - displaying elements for saving the search
48 *
6a488035
TO
49 *
50 * @return void
51 */
00be9182 52 public function buildQuickForm() {
6a488035
TO
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
20f965bb
CW
58 // Values from the search form
59 $formValues = $this->controller->exportValues();
60
6a488035
TO
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)) {
be2fb01f 76 $params = ['saved_search_id' => $this->_id];
6a488035
TO
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'));
20f965bb 84 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
6a488035
TO
85 }
86
87 $this->addRule('title', ts('Name already exists in Database.'),
be2fb01f 88 'objectExists', ['CRM_Contact_DAO_Group', $groupId, 'title']
6a488035
TO
89 );
90 }
91
92 /**
66f9e52b 93 * Process the form after the input has been submitted and validated.
6a488035 94 *
6a488035
TO
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'));
6a488035
TO
108 $savedSearch->save();
109 $this->set('ssID', $savedSearch->id);
be2fb01f 110 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Saved'), 'success');
6a488035
TO
111
112 // also create a group that is associated with this saved search only if new saved search
be2fb01f 113 $params = [];
6a488035
TO
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 }
96025800 125
6a488035 126}