58d2a9efc68defcd3a9b3fd745dc3241ebc24a06
[civicrm-core.git] / CRM / Contact / Page / SavedSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Main page for viewing all Saved searches.
38 *
39 */
40 class CRM_Contact_Page_SavedSearch extends CRM_Core_Page {
41
42 /**
43 * The action links that we need to display for the browse screen
44 *
45 * @var array
46 * @static
47 */
48 static $_links = NULL;
49
50 /**
51 * Delete a saved search.
52 *
53 * @param int $id
54 * Id of saved search.
55 *
56 * @return void
57 *
58 */
59 public function delete($id) {
60 // first delete the group associated with this saved search
61 $group = new CRM_Contact_DAO_Group();
62 $group->saved_search_id = $id;
63 if ($group->find(TRUE)) {
64 CRM_Contact_BAO_Group::discard($group->id);
65 }
66
67 $savedSearch = new CRM_Contact_DAO_SavedSearch();
68 $savedSearch->id = $id;
69 $savedSearch->is_active = 0;
70 $savedSearch->save();
71 return;
72 }
73
74 /**
75 * Browse all saved searches.
76 *
77 * @return content of the parents run method
78 *
79 */
80 public function browse() {
81 $rows = array();
82
83 $savedSearch = new CRM_Contact_DAO_SavedSearch();
84 $savedSearch->is_active = 1;
85 $savedSearch->selectAdd();
86 $savedSearch->selectAdd('id, form_values');
87 $savedSearch->find();
88 $properties = array('id', 'name', 'description');
89 while ($savedSearch->fetch()) {
90 // get name and description from group object
91 $group = new CRM_Contact_DAO_Group();
92 $group->saved_search_id = $savedSearch->id;
93 if ($group->find(TRUE)) {
94 $permissions = CRM_Group_Page_Group::checkPermission($group->id, $group->title);
95 if (!CRM_Utils_System::isNull($permissions)) {
96 $row = array();
97
98 $row['name'] = $group->title;
99 $row['description'] = $group->description;
100
101 $row['id'] = $savedSearch->id;
102 $formValues = unserialize($savedSearch->form_values);
103 $query = new CRM_Contact_BAO_Query($formValues);
104 $row['query_detail'] = $query->qill();
105
106 $action = array_sum(array_keys(self::links()));
107 $action = $action & CRM_Core_Action::mask($permissions);
108 $row['action'] = CRM_Core_Action::formLink(
109 self::links(),
110 $action,
111 array('id' => $row['id']),
112 ts('more'),
113 FALSE,
114 'savedSearch.manage.action',
115 'SavedSearch',
116 $row['id']
117 );
118
119 $rows[] = $row;
120 }
121 }
122 }
123
124 $this->assign('rows', $rows);
125 return parent::run();
126 }
127
128 /**
129 * Run this page (figure out the action needed and perform it).
130 *
131 * @return void
132 */
133 public function run() {
134 $action = CRM_Utils_Request::retrieve('action', 'String',
135 $this, FALSE, 'browse'
136 );
137
138 $this->assign('action', $action);
139
140 if ($action & CRM_Core_Action::DELETE) {
141 $id = CRM_Utils_Request::retrieve('id', 'Positive',
142 $this, TRUE
143 );
144 $this->delete($id);
145 }
146 $this->browse();
147 }
148
149 /**
150 * Get action Links
151 *
152 * @return array (reference) of action links
153 * @static
154 */
155 public static function &links() {
156
157 if (!(self::$_links)) {
158
159 $deleteExtra = ts('Do you really want to remove this Smart Group?');
160
161 self::$_links = array(
162 CRM_Core_Action::VIEW => array(
163 'name' => ts('Search'),
164 'url' => 'civicrm/contact/search/advanced',
165 'qs' => 'reset=1&force=1&ssID=%%id%%',
166 'title' => ts('Search'),
167 ),
168 CRM_Core_Action::DELETE => array(
169 'name' => ts('Delete'),
170 'url' => 'civicrm/contact/search/saved',
171 'qs' => 'action=delete&id=%%id%%',
172 'extra' => 'onclick="return confirm(\'' . $deleteExtra . '\');"',
173 ),
174 );
175 }
176 return self::$_links;
177 }
178 }