| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Page for displaying list of categories |
| 38 | */ |
| 39 | class CRM_Admin_Page_Tag extends CRM_Core_Page_Basic { |
| 40 | |
| 41 | public $useLivePageJS = TRUE; |
| 42 | |
| 43 | /** |
| 44 | * The action links that we need to display for the browse screen |
| 45 | * |
| 46 | * @var array |
| 47 | * @static |
| 48 | */ |
| 49 | static $_links = NULL; |
| 50 | |
| 51 | /** |
| 52 | * Get BAO |
| 53 | * |
| 54 | * @return string Classname of BAO. |
| 55 | */ |
| 56 | function getBAOName() { |
| 57 | return 'CRM_Core_BAO_Tag'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get action Links |
| 62 | * |
| 63 | * @return array (reference) of action links |
| 64 | */ |
| 65 | function &links() { |
| 66 | if (!(self::$_links)) { |
| 67 | self::$_links = array( |
| 68 | CRM_Core_Action::UPDATE => array( |
| 69 | 'name' => ts('Edit'), |
| 70 | 'url' => 'civicrm/admin/tag', |
| 71 | 'qs' => 'action=update&id=%%id%%&reset=1', |
| 72 | 'title' => ts('Edit Tag'), |
| 73 | ), |
| 74 | CRM_Core_Action::DELETE => array( |
| 75 | 'name' => ts('Delete'), |
| 76 | 'url' => 'civicrm/admin/tag', |
| 77 | 'qs' => 'action=delete&id=%%id%%', |
| 78 | 'title' => ts('Delete Tag'), |
| 79 | ), |
| 80 | CRM_Core_Action::FOLLOWUP => array( |
| 81 | 'name' => ts('Merge'), |
| 82 | 'class' => 'merge_tag', |
| 83 | 'title' => ts('Merge Tag'), |
| 84 | ), |
| 85 | ); |
| 86 | } |
| 87 | return self::$_links; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get name of edit form |
| 92 | * |
| 93 | * @return string Classname of edit form. |
| 94 | */ |
| 95 | function editForm() { |
| 96 | return 'CRM_Admin_Form_Tag'; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get form name for edit form |
| 101 | * |
| 102 | * @return string name of this page. |
| 103 | */ |
| 104 | function editName() { |
| 105 | return 'Tag'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get form name for delete form |
| 110 | * |
| 111 | * @return string name of this page. |
| 112 | */ |
| 113 | function deleteName() { |
| 114 | return 'Tag'; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get user context. |
| 119 | * |
| 120 | * @return string user context. |
| 121 | */ |
| 122 | function userContext($mode = NULL) { |
| 123 | return 'civicrm/admin/tag'; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get name of delete form |
| 128 | * |
| 129 | * @return string Classname of delete form. |
| 130 | */ |
| 131 | function deleteForm() { |
| 132 | return 'CRM_Admin_Form_Tag'; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * override function browse() |
| 137 | */ |
| 138 | function browse($action = NULL, $sort = NULL) { |
| 139 | $adminTagSet = FALSE; |
| 140 | if (CRM_Core_Permission::check('administer Tagsets')) { |
| 141 | $adminTagSet = TRUE; |
| 142 | } |
| 143 | $this->assign('adminTagSet', $adminTagSet); |
| 144 | |
| 145 | $reservedClause = !CRM_Core_Permission::check('administer reserved tags') ? "AND t1.is_reserved != 1" : ''; |
| 146 | $query = "SELECT t1.name, t1.id |
| 147 | FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.id = t2.parent_id |
| 148 | WHERE t2.id IS NULL {$reservedClause}"; |
| 149 | $tag = CRM_Core_DAO::executeQuery($query); |
| 150 | |
| 151 | $mergeableTags = array(); |
| 152 | while ($tag->fetch()) { |
| 153 | $mergeableTags[$tag->id] = 1; |
| 154 | } |
| 155 | |
| 156 | $usedFor = CRM_Core_OptionGroup::values('tag_used_for'); |
| 157 | |
| 158 | $query = "SELECT t1.name, t1.id, t2.name as parent, t1.description, t1.used_for, t1.is_tagset, |
| 159 | t1.is_reserved, t1.parent_id, t1.used_for |
| 160 | FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id |
| 161 | GROUP BY t1.parent_id, t1.id"; |
| 162 | |
| 163 | $tag = CRM_Core_DAO::executeQuery($query); |
| 164 | $values = array(); |
| 165 | |
| 166 | $action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE; |
| 167 | $permission = CRM_Core_Permission::EDIT; |
| 168 | |
| 169 | while ($tag->fetch()) { |
| 170 | $values[$tag->id] = (array) $tag; |
| 171 | |
| 172 | $used = array(); |
| 173 | if ($values[$tag->id]['used_for']) { |
| 174 | $usedArray = explode(",", $values[$tag->id]['used_for']); |
| 175 | foreach ($usedArray as $key => $value) { |
| 176 | $used[$key] = $usedFor[$value]; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!empty($used)) { |
| 181 | $values[$tag->id]['used_for'] = implode(", ", $used); |
| 182 | } |
| 183 | |
| 184 | $newAction = $action; |
| 185 | if ($values[$tag->id]['is_reserved']) { |
| 186 | $newAction = CRM_Core_Action::UPDATE; |
| 187 | } |
| 188 | |
| 189 | if ($values[$tag->id]['is_tagset'] && !CRM_Core_Permission::check('administer Tagsets')) { |
| 190 | $newAction = 0; |
| 191 | } |
| 192 | |
| 193 | if (array_key_exists($tag->id, $mergeableTags)) { |
| 194 | $newAction += CRM_Core_Action::FOLLOWUP; |
| 195 | } |
| 196 | |
| 197 | // populate action links |
| 198 | if ($newAction) { |
| 199 | $this->action($tag, $newAction, $values[$tag->id], self::links(), $permission, TRUE); |
| 200 | } |
| 201 | else { |
| 202 | $values[$tag->id]['action'] = ''; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | $this->assign('rows', $values); |
| 207 | } |
| 208 | } |
| 209 | |