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