| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Main page for viewing all Saved searches. |
| 20 | */ |
| 21 | class CRM_Contact_Page_SavedSearch extends CRM_Core_Page { |
| 22 | |
| 23 | /** |
| 24 | * The action links that we need to display for the browse screen. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | public static $_links = NULL; |
| 29 | |
| 30 | /** |
| 31 | * Delete a saved search. |
| 32 | * |
| 33 | * @param int $id |
| 34 | * Id of saved search. |
| 35 | */ |
| 36 | public function delete($id) { |
| 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(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Browse all saved searches. |
| 52 | * |
| 53 | * @return mixed |
| 54 | * content of the parents run method |
| 55 | */ |
| 56 | public function browse() { |
| 57 | $rows = []; |
| 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(); |
| 64 | $properties = ['id', 'name', 'description']; |
| 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)) { |
| 70 | $permissions = CRM_Contact_BAO_Group::checkPermission($group->id, TRUE); |
| 71 | if (!CRM_Utils_System::isNull($permissions)) { |
| 72 | $row = []; |
| 73 | |
| 74 | $row['name'] = $group->title; |
| 75 | $row['description'] = $group->description; |
| 76 | |
| 77 | $row['id'] = $savedSearch->id; |
| 78 | $formValues = CRM_Utils_String::unserialize($savedSearch->form_values); |
| 79 | $query = new CRM_Contact_BAO_Query($formValues); |
| 80 | $row['query_detail'] = $query->qill(); |
| 81 | |
| 82 | $action = array_sum(array_keys(self::links())); |
| 83 | $action = $action & CRM_Core_Action::mask($permissions); |
| 84 | $row['action'] = CRM_Core_Action::formLink( |
| 85 | self::links(), |
| 86 | $action, |
| 87 | ['id' => $row['id']], |
| 88 | ts('more'), |
| 89 | FALSE, |
| 90 | 'savedSearch.manage.action', |
| 91 | 'SavedSearch', |
| 92 | $row['id'] |
| 93 | ); |
| 94 | |
| 95 | $rows[] = $row; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $this->assign('rows', $rows); |
| 101 | return parent::run(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Run this page (figure out the action needed and perform it). |
| 106 | */ |
| 107 | public function run() { |
| 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 | /** |
| 124 | * Get action Links. |
| 125 | * |
| 126 | * @return array |
| 127 | * (reference) of action links |
| 128 | */ |
| 129 | public static function &links() { |
| 130 | |
| 131 | if (!(self::$_links)) { |
| 132 | |
| 133 | $deleteExtra = ts('Do you really want to remove this Smart Group?'); |
| 134 | |
| 135 | self::$_links = [ |
| 136 | CRM_Core_Action::VIEW => [ |
| 137 | 'name' => ts('Search'), |
| 138 | 'url' => 'civicrm/contact/search/advanced', |
| 139 | 'qs' => 'reset=1&force=1&ssID=%%id%%', |
| 140 | 'title' => ts('Search'), |
| 141 | ], |
| 142 | CRM_Core_Action::DELETE => [ |
| 143 | 'name' => ts('Delete'), |
| 144 | 'url' => 'civicrm/contact/search/saved', |
| 145 | 'qs' => 'action=delete&id=%%id%%', |
| 146 | 'extra' => 'onclick="return confirm(\'' . $deleteExtra . '\');"', |
| 147 | ], |
| 148 | ]; |
| 149 | } |
| 150 | return self::$_links; |
| 151 | } |
| 152 | |
| 153 | } |