(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Contact / Page / SavedSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Main page for viewing all Saved searches.
6a488035
TO
20 */
21class CRM_Contact_Page_SavedSearch extends CRM_Core_Page {
22
23 /**
fe482240 24 * The action links that we need to display for the browse screen.
6a488035
TO
25 *
26 * @var array
6a488035 27 */
69078420 28 public static $_links = NULL;
6a488035
TO
29
30 /**
100fef9d 31 * Delete a saved search.
6a488035 32 *
77c5b619
TO
33 * @param int $id
34 * Id of saved search.
6a488035 35 */
00be9182 36 public function delete($id) {
6a488035
TO
37 // first delete the group associated with this saved search
38 $group = new CRM_Contact_DAO_Group();
39 $group->saved_search_id = $id;
40 if ($group->find(TRUE)) {
41 CRM_Contact_BAO_Group::discard($group->id);
42 }
43
44 $savedSearch = new CRM_Contact_DAO_SavedSearch();
45 $savedSearch->id = $id;
46 $savedSearch->is_active = 0;
47 $savedSearch->save();
6a488035
TO
48 }
49
50 /**
51 * Browse all saved searches.
52 *
72b3a70c
CW
53 * @return mixed
54 * content of the parents run method
6a488035 55 */
00be9182 56 public function browse() {
be2fb01f 57 $rows = [];
6a488035
TO
58
59 $savedSearch = new CRM_Contact_DAO_SavedSearch();
60 $savedSearch->is_active = 1;
61 $savedSearch->selectAdd();
62 $savedSearch->selectAdd('id, form_values');
63 $savedSearch->find();
be2fb01f 64 $properties = ['id', 'name', 'description'];
6a488035
TO
65 while ($savedSearch->fetch()) {
66 // get name and description from group object
67 $group = new CRM_Contact_DAO_Group();
68 $group->saved_search_id = $savedSearch->id;
69 if ($group->find(TRUE)) {
5355941d 70 $permissions = CRM_Contact_BAO_Group::checkPermission($group->id, TRUE);
6a488035 71 if (!CRM_Utils_System::isNull($permissions)) {
be2fb01f 72 $row = [];
6a488035
TO
73
74 $row['name'] = $group->title;
75 $row['description'] = $group->description;
76
353ffa53
TO
77 $row['id'] = $savedSearch->id;
78 $formValues = unserialize($savedSearch->form_values);
79 $query = new CRM_Contact_BAO_Query($formValues);
6a488035
TO
80 $row['query_detail'] = $query->qill();
81
353ffa53
TO
82 $action = array_sum(array_keys(self::links()));
83 $action = $action & CRM_Core_Action::mask($permissions);
87dab4a4
AH
84 $row['action'] = CRM_Core_Action::formLink(
85 self::links(),
86 $action,
be2fb01f 87 ['id' => $row['id']],
87dab4a4
AH
88 ts('more'),
89 FALSE,
90 'savedSearch.manage.action',
91 'SavedSearch',
92 $row['id']
93 );
6a488035
TO
94
95 $rows[] = $row;
96 }
97 }
98 }
99
100 $this->assign('rows', $rows);
101 return parent::run();
102 }
103
104 /**
100fef9d 105 * Run this page (figure out the action needed and perform it).
6a488035 106 */
00be9182 107 public function run() {
6a488035
TO
108 $action = CRM_Utils_Request::retrieve('action', 'String',
109 $this, FALSE, 'browse'
110 );
111
112 $this->assign('action', $action);
113
114 if ($action & CRM_Core_Action::DELETE) {
115 $id = CRM_Utils_Request::retrieve('id', 'Positive',
116 $this, TRUE
117 );
118 $this->delete($id);
119 }
120 $this->browse();
121 }
122
123 /**
fe482240 124 * Get action Links.
6a488035 125 *
a6c01b45
CW
126 * @return array
127 * (reference) of action links
6a488035 128 */
00be9182 129 public static function &links() {
6a488035
TO
130
131 if (!(self::$_links)) {
132
133 $deleteExtra = ts('Do you really want to remove this Smart Group?');
134
be2fb01f
CW
135 self::$_links = [
136 CRM_Core_Action::VIEW => [
6a488035
TO
137 'name' => ts('Search'),
138 'url' => 'civicrm/contact/search/advanced',
139 'qs' => 'reset=1&force=1&ssID=%%id%%',
140 'title' => ts('Search'),
be2fb01f
CW
141 ],
142 CRM_Core_Action::DELETE => [
6a488035
TO
143 'name' => ts('Delete'),
144 'url' => 'civicrm/contact/search/saved',
145 'qs' => 'action=delete&id=%%id%%',
146 'extra' => 'onclick="return confirm(\'' . $deleteExtra . '\');"',
be2fb01f
CW
147 ],
148 ];
6a488035
TO
149 }
150 return self::$_links;
151 }
96025800 152
6a488035 153}