Merge pull request #4944 from civicrm/batch-5.part2
[civicrm-core.git] / CRM / Event / Form / Task / SaveSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
100fef9d 44 * Saved search id if any
6a488035
TO
45 *
46 * @var int
47 */
48 protected $_id;
49
50 /**
100fef9d 51 * Build all the data structures needed to build the form
6a488035
TO
52 *
53 * @return void
95ea96be
EM
54 */
55 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
74 // need to save qill for the smarty template
75 $this->assign('qill', $qill);
76
77 // the name and description are actually stored with the group and not the saved search
78 $this->add('text', 'title', ts('Name'),
79 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
80 );
81
82 $this->addElement('text', 'description', ts('Description'),
83 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
84 );
85
86 // get the group id for the saved search
87 $groupId = NULL;
88 if (isset($this->_id)) {
89 $params = array('saved_search_id' => $this->_id);
90 CRM_Contact_BAO_Group::retrieve($params, $values);
91 $groupId = $values['id'];
92
93 $this->addDefaultButtons(ts('Update Smart Group'));
94 }
95 else {
96 $this->addDefaultButtons(ts('Save Smart Group'));
97 }
98
99 $this->addRule('title', ts('Name already exists in Database.'),
100 'objectExists', array('CRM_Contact_DAO_Group', $groupId, 'title')
101 );
102 }
103
104 /**
100fef9d 105 * Process the form after the input has been submitted and validated
6a488035 106 *
6a488035
TO
107 *
108 * @return void
109 */
110 public function postProcess() {
111 // saved search form values
112 $formValues = $this->controller->exportValues();
113
114 $session = CRM_Core_Session::singleton();
115
116 //save the search
117 $savedSearch = new CRM_Contact_BAO_SavedSearch();
118 $savedSearch->id = $this->_id;
119 $savedSearch->form_values = serialize($this->get('formValues'));
120 $savedSearch->mapping_id = $mappingId;
121 $savedSearch->save();
122 $this->set('ssID', $savedSearch->id);
123 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Saved'), 'success');
124
125 // also create a group that is associated with this saved search only if new saved search
126 $params = array();
127 $params['title'] = $formValues['title'];
128 $params['description'] = $formValues['description'];
129 $params['visibility'] = 'User and User Admin Only';
130 $params['saved_search_id'] = $savedSearch->id;
131 $params['is_active'] = 1;
132
133 if ($this->_id) {
134 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
135 }
136 $group = CRM_Contact_BAO_Group::create($params);
137 }
138}