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