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