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