3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Core_ShowHideBlocks
{
38 * The icons prefixed to block show and hide links.
42 static $_showIcon, $_hideIcon;
45 * The array of ids of blocks that will be shown
52 * The array of ids of blocks that will be hidden
62 * Initial value of show array.
64 * Initial value of hide array.
66 * @return \CRM_Core_ShowHideBlocks the newly created object
69 public function __construct($show = NULL, $hide = NULL) {
74 $this->_show
= array();
81 $this->_hide
= array();
86 * Load icon vars used in hide and show links
91 public static function setIcons() {
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') . '"/>';
100 * Add the values from this class to the template
104 public function addToTemplate() {
108 foreach (array_keys($this->_hide
) as $h) {
117 foreach (array_keys($this->_show
) as $s) {
125 $template = CRM_Core_Smarty
::singleton();
126 $template->assign_by_ref('hideBlocks', $hide);
127 $template->assign_by_ref('showBlocks', $show);
131 * Add a value to the show array
133 * @param string $name
138 public function addShow($name) {
139 $this->_show
[$name] = 1;
140 if (array_key_exists($name, $this->_hide
)) {
141 unset($this->_hide
[$name]);
146 * Add a value to the hide array
148 * @param string $name
153 public function addHide($name) {
154 $this->_hide
[$name] = 1;
155 if (array_key_exists($name, $this->_show
)) {
156 unset($this->_show
[$name]);
161 * Create a well formatted html link from the smaller pieces
163 * @param string $name
165 * @param string $href
166 * @param string $text
170 * the formatted html link
172 public static function linkHtml($name, $href, $text, $js) {
173 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
177 * Create links that we can use in the form
179 * @param CRM_Core_Form $form
181 * @param string $prefix
182 * The attribute that we are referencing.
183 * @param string $showLinkText
184 * The text to be shown for the show link.
185 * @param string $hideLinkText
186 * The text to be shown for the hide link.
188 * @param bool $assign
194 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
195 $showCode = "cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();";
196 $hideCode = "cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show(); return false;";
200 $values['show'] = self
::linkHtml("${prefix}_show", "#${prefix}_hide", self
::$_showIcon . $showLinkText, "onclick=\"$showCode\"");
201 $values['hide'] = self
::linkHtml("${prefix}_hide", "#${prefix}", self
::$_hideIcon . $hideLinkText, "onclick=\"$hideCode\"");
204 $form->assign($prefix, $values);
212 * Create html link elements that we can use in the form
214 * @param CRM_Core_Form $form
217 * The current index of the element being processed.
218 * @param int $maxIndex
219 * The max number of elements that will be processed.
220 * @param string $prefix
221 * The attribute that we are referencing.
222 * @param string $showLinkText
223 * The text to be shown for the show link.
224 * @param string $hideLinkText
225 * The text to be shown for the hide link.
226 * @param string $elementType
228 * @param string $hideLink
229 * The hide block string.
233 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
234 $showHidePrefix = str_replace(array("]", "["), array("", "_"), $prefix);
235 $showHidePrefix = "id_" . $showHidePrefix;
236 if ($index == $maxIndex) {
237 $showCode = $hideCode = "return false;";
242 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
244 $hideCode = $hideLink;
247 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
251 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
252 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
258 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
259 array('onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode)
261 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
262 array('onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode)
266 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
267 array('onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode)
269 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
270 array('onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode)