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