Pass cachekey around more & criteria less
[civicrm-core.git] / CRM / Contact / Page / SavedSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
35 * Main page for viewing all Saved searches.
6a488035
TO
36 */
37class CRM_Contact_Page_SavedSearch extends CRM_Core_Page {
38
39 /**
fe482240 40 * The action links that we need to display for the browse screen.
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 static $_links = NULL;
45
46 /**
100fef9d 47 * Delete a saved search.
6a488035 48 *
77c5b619
TO
49 * @param int $id
50 * Id of saved search.
6a488035 51 */
00be9182 52 public function delete($id) {
6a488035
TO
53 // first delete the group associated with this saved search
54 $group = new CRM_Contact_DAO_Group();
55 $group->saved_search_id = $id;
56 if ($group->find(TRUE)) {
57 CRM_Contact_BAO_Group::discard($group->id);
58 }
59
60 $savedSearch = new CRM_Contact_DAO_SavedSearch();
61 $savedSearch->id = $id;
62 $savedSearch->is_active = 0;
63 $savedSearch->save();
6a488035
TO
64 }
65
66 /**
67 * Browse all saved searches.
68 *
72b3a70c
CW
69 * @return mixed
70 * content of the parents run method
6a488035 71 */
00be9182 72 public function browse() {
6a488035
TO
73 $rows = array();
74
75 $savedSearch = new CRM_Contact_DAO_SavedSearch();
76 $savedSearch->is_active = 1;
77 $savedSearch->selectAdd();
78 $savedSearch->selectAdd('id, form_values');
79 $savedSearch->find();
80 $properties = array('id', 'name', 'description');
81 while ($savedSearch->fetch()) {
82 // get name and description from group object
83 $group = new CRM_Contact_DAO_Group();
84 $group->saved_search_id = $savedSearch->id;
85 if ($group->find(TRUE)) {
5355941d 86 $permissions = CRM_Contact_BAO_Group::checkPermission($group->id, TRUE);
6a488035
TO
87 if (!CRM_Utils_System::isNull($permissions)) {
88 $row = array();
89
90 $row['name'] = $group->title;
91 $row['description'] = $group->description;
92
353ffa53
TO
93 $row['id'] = $savedSearch->id;
94 $formValues = unserialize($savedSearch->form_values);
95 $query = new CRM_Contact_BAO_Query($formValues);
6a488035
TO
96 $row['query_detail'] = $query->qill();
97
353ffa53
TO
98 $action = array_sum(array_keys(self::links()));
99 $action = $action & CRM_Core_Action::mask($permissions);
87dab4a4
AH
100 $row['action'] = CRM_Core_Action::formLink(
101 self::links(),
102 $action,
103 array('id' => $row['id']),
104 ts('more'),
105 FALSE,
106 'savedSearch.manage.action',
107 'SavedSearch',
108 $row['id']
109 );
6a488035
TO
110
111 $rows[] = $row;
112 }
113 }
114 }
115
116 $this->assign('rows', $rows);
117 return parent::run();
118 }
119
120 /**
100fef9d 121 * Run this page (figure out the action needed and perform it).
6a488035 122 */
00be9182 123 public function run() {
6a488035
TO
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 /**
fe482240 140 * Get action Links.
6a488035 141 *
a6c01b45
CW
142 * @return array
143 * (reference) of action links
6a488035 144 */
00be9182 145 public static function &links() {
6a488035
TO
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 }
96025800 168
6a488035 169}