dev/core#2017 remove unused SearchTaskHookSample.php
[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 */
17
18/**
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
22 */
23class CRM_Event_Form_Task_SaveSearch extends CRM_Event_Form_Task {
24
25 /**
66f9e52b 26 * Saved search id if any.
6a488035
TO
27 *
28 * @var int
29 */
30 protected $_id;
31
32 /**
66f9e52b 33 * Build all the data structures needed to build the form.
6a488035
TO
34 *
35 * @return void
95ea96be 36 */
79d7553f 37 public function preProcess() {
6a488035
TO
38 parent::preProcess();
39 $this->_id = NULL;
40 }
41
42 /**
c490a46a 43 * Build the form object - it consists of
6a488035
TO
44 * - displaying the QILL (query in local language)
45 * - displaying elements for saving the search
46 *
6a488035
TO
47 *
48 * @return void
49 */
00be9182 50 public function buildQuickForm() {
6a488035
TO
51 CRM_Utils_System::setTitle(ts('Smart Group'));
52 // get the qill
53 $query = new CRM_Event_BAO_Query($this->get('formValues'));
54 $qill = $query->qill();
55
20f965bb
CW
56 // Values from the search form
57 $formValues = $this->controller->exportValues();
58
6a488035
TO
59 // need to save qill for the smarty template
60 $this->assign('qill', $qill);
61
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
65 );
66
67 $this->addElement('text', 'description', ts('Description'),
68 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
69 );
70
71 // get the group id for the saved search
72 $groupId = NULL;
73 if (isset($this->_id)) {
be2fb01f 74 $params = ['saved_search_id' => $this->_id];
6a488035
TO
75 CRM_Contact_BAO_Group::retrieve($params, $values);
76 $groupId = $values['id'];
77
78 $this->addDefaultButtons(ts('Update Smart Group'));
79 }
80 else {
81 $this->addDefaultButtons(ts('Save Smart Group'));
20f965bb 82 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
6a488035
TO
83 }
84
85 $this->addRule('title', ts('Name already exists in Database.'),
be2fb01f 86 'objectExists', ['CRM_Contact_DAO_Group', $groupId, 'title']
6a488035
TO
87 );
88 }
89
90 /**
66f9e52b 91 * Process the form after the input has been submitted and validated.
6a488035 92 *
6a488035
TO
93 *
94 * @return void
95 */
96 public function postProcess() {
97 // saved search form values
98 $formValues = $this->controller->exportValues();
99
6a488035
TO
100 //save the search
101 $savedSearch = new CRM_Contact_BAO_SavedSearch();
102 $savedSearch->id = $this->_id;
56d513f2 103 $savedSearch->form_values = serialize($this->get('queryParams'));
6a488035
TO
104 $savedSearch->save();
105 $this->set('ssID', $savedSearch->id);
be2fb01f 106 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Saved'), 'success');
6a488035
TO
107
108 // also create a group that is associated with this saved search only if new saved search
be2fb01f 109 $params = [];
6a488035
TO
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;
115
116 if ($this->_id) {
117 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
118 }
119 $group = CRM_Contact_BAO_Group::create($params);
120 }
96025800 121
6a488035 122}