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