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