Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Core / ShowHideBlocks.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 */
35class CRM_Core_ShowHideBlocks {
36
37 /**
38 * The icons prefixed to block show and hide links.
39 *
40 * @var string
41 */
42 static $_showIcon, $_hideIcon;
43
44 /**
45 * The array of ids of blocks that will be shown
46 *
47 * @var array
48 */
49 protected $_show;
50
51 /**
52 * The array of ids of blocks that will be hidden
53 *
54 * @var array
55 */
56 protected $_hide;
57
58 /**
100fef9d 59 * Class constructor
6a488035 60 *
6a0b768e
TO
61 * @param array $show
62 * Initial value of show array.
63 * @param array $hide
64 * Initial value of hide array.
6a488035 65 *
e0b82b44
CW
66 * @return \CRM_Core_ShowHideBlocks the newly created object
67 * @access public
6a488035 68 */
00be9182 69 public function __construct($show = NULL, $hide = NULL) {
6a488035
TO
70 if (!empty($show)) {
71 $this->_show = $show;
72 }
73 else {
74 $this->_show = array();
75 }
76
77 if (!empty($hide)) {
78 $this->_hide = $hide;
79 }
80 else {
81 $this->_hide = array();
82 }
83 }
84
85 /**
100fef9d 86 * Load icon vars used in hide and show links
6a488035
TO
87 *
88 * @return void
6a488035
TO
89 * @static
90 */
00be9182 91 public static function setIcons() {
6a488035
TO
92 if (!isset(self::$_showIcon)) {
93 $config = CRM_Core_Config::singleton();
94 self::$_showIcon = '<img src="' . $config->resourceBase . 'i/TreePlus.gif" class="action-icon" alt="' . ts('show field or section') . '"/>';
95 self::$_hideIcon = '<img src="' . $config->resourceBase . 'i/TreeMinus.gif" class="action-icon" alt="' . ts('hide field or section') . '"/>';
96 }
97 }
98
99 /**
100fef9d 100 * Add the values from this class to the template
6a488035
TO
101 *
102 * @return void
6a488035 103 */
00be9182 104 public function addToTemplate() {
6a488035
TO
105 $hide = $show = '';
106
107 $first = TRUE;
108 foreach (array_keys($this->_hide) as $h) {
109 if (!$first) {
110 $hide .= ',';
111 }
112 $hide .= "'$h'";
113 $first = FALSE;
114 }
115
116 $first = TRUE;
117 foreach (array_keys($this->_show) as $s) {
118 if (!$first) {
119 $show .= ',';
120 }
121 $show .= "'$s'";
122 $first = FALSE;
123 }
124
125 $template = CRM_Core_Smarty::singleton();
126 $template->assign_by_ref('hideBlocks', $hide);
127 $template->assign_by_ref('showBlocks', $show);
128 }
129
130 /**
131 * Add a value to the show array
132 *
6a0b768e
TO
133 * @param string $name
134 * Id to be added.
6a488035
TO
135 *
136 * @return void
6a488035 137 */
00be9182 138 public function addShow($name) {
6a488035
TO
139 $this->_show[$name] = 1;
140 if (array_key_exists($name, $this->_hide)) {
141 unset($this->_hide[$name]);
142 }
143 }
144
145 /**
146 * Add a value to the hide array
147 *
6a0b768e
TO
148 * @param string $name
149 * Id to be added.
6a488035
TO
150 *
151 * @return void
6a488035 152 */
00be9182 153 public function addHide($name) {
6a488035
TO
154 $this->_hide[$name] = 1;
155 if (array_key_exists($name, $this->_show)) {
156 unset($this->_show[$name]);
157 }
158 }
159
160 /**
100fef9d 161 * Create a well formatted html link from the smaller pieces
6a488035 162 *
6a0b768e
TO
163 * @param string $name
164 * Name of the link.
6a488035
TO
165 * @param string $href
166 * @param string $text
167 * @param string $js
168 *
169 * @return string the formatted html link
6a488035 170 */
00be9182 171 public static function linkHtml($name, $href, $text, $js) {
6a488035
TO
172 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
173 }
174
175 /**
176 * Create links that we can use in the form
177 *
6a0b768e
TO
178 * @param CRM_Core_Form $form
179 * The form object.
180 * @param string $prefix
181 * The attribute that we are referencing.
182 * @param string $showLinkText
183 * The text to be shown for the show link.
184 * @param string $hideLinkText
185 * The text to be shown for the hide link.
fd31fa4c
EM
186 *
187 * @param bool $assign
6a488035
TO
188 *
189 * @static
190 *
191 * @return void
6a488035 192 */
00be9182 193 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
6a488035
TO
194 $showCode = "cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();";
195 $hideCode = "cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show(); return false;";
196
197 self::setIcons();
198 $values = array();
199 $values['show'] = self::linkHtml("${prefix}_show", "#${prefix}_hide", self::$_showIcon . $showLinkText, "onclick=\"$showCode\"");
200 $values['hide'] = self::linkHtml("${prefix}_hide", "#${prefix}", self::$_hideIcon . $hideLinkText, "onclick=\"$hideCode\"");
201
202 if ($assign) {
203 $form->assign($prefix, $values);
204 }
205 else {
206 return $values;
207 }
208 }
209
210 /**
211 * Create html link elements that we can use in the form
212 *
6a0b768e
TO
213 * @param CRM_Core_Form $form
214 * The form object.
215 * @param int $index
216 * The current index of the element being processed.
217 * @param int $maxIndex
218 * The max number of elements that will be processed.
219 * @param string $prefix
220 * The attribute that we are referencing.
221 * @param string $showLinkText
222 * The text to be shown for the show link.
223 * @param string $hideLinkText
224 * The text to be shown for the hide link.
225 * @param string $elementType
226 * The set the class.
227 * @param string $hideLink
228 * The hide block string.
6a488035
TO
229 *
230 * @return void
6a488035 231 */
00be9182 232 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
6a488035
TO
233 $showHidePrefix = str_replace(array("]", "["), array("", "_"), $prefix);
234 $showHidePrefix = "id_" . $showHidePrefix;
235 if ($index == $maxIndex) {
236 $showCode = $hideCode = "return false;";
237 }
238 else {
239 $next = $index + 1;
240 if ($elementType) {
241 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
242 if ($hideLink) {
243 $hideCode = $hideLink;
244 }
245 else {
246 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
247 }
248 }
249 else {
250 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
251 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
252 }
253 }
254
255 self::setIcons();
256 if ($elementType) {
257 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self::$_showIcon . $showLinkText,
258 array('onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode)
259 );
260 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText,
261 array('onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode)
262 );
263 }
264 else {
265 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self::$_showIcon . $showLinkText,
266 array('onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode)
267 );
268 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText,
269 array('onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode)
270 );
271 }
272 }
273}