3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Core_ShowHideBlocks
{
36 * The icons prefixed to block show and hide links.
40 static $_showIcon, $_hideIcon;
43 * The array of ids of blocks that will be shown.
50 * The array of ids of blocks that will be hidden.
60 * Initial value of show array.
62 * Initial value of hide array.
64 * @return \CRM_Core_ShowHideBlocks the newly created object
66 public function __construct($show = NULL, $hide = NULL) {
71 $this->_show
= array();
78 $this->_hide
= array();
83 * Load icon vars used in hide and show links.
85 public static function setIcons() {
86 if (!isset(self
::$_showIcon)) {
87 $config = CRM_Core_Config
::singleton();
88 self
::$_showIcon = '<img src="' . $config->resourceBase
. 'i/TreePlus.gif" class="action-icon" alt="' . ts('show field or section') . '"/>';
89 self
::$_hideIcon = '<img src="' . $config->resourceBase
. 'i/TreeMinus.gif" class="action-icon" alt="' . ts('hide field or section') . '"/>';
94 * Add the values from this class to the template.
96 public function addToTemplate() {
100 foreach (array_keys($this->_hide
) as $h) {
109 foreach (array_keys($this->_show
) as $s) {
117 $template = CRM_Core_Smarty
::singleton();
118 $template->assign_by_ref('hideBlocks', $hide);
119 $template->assign_by_ref('showBlocks', $show);
123 * Add a value to the show array.
125 * @param string $name
128 public function addShow($name) {
129 $this->_show
[$name] = 1;
130 if (array_key_exists($name, $this->_hide
)) {
131 unset($this->_hide
[$name]);
136 * Add a value to the hide array.
138 * @param string $name
141 public function addHide($name) {
142 $this->_hide
[$name] = 1;
143 if (array_key_exists($name, $this->_show
)) {
144 unset($this->_show
[$name]);
149 * Create a well formatted html link from the smaller pieces.
151 * @param string $name
153 * @param string $href
154 * @param string $text
158 * the formatted html link
160 public static function linkHtml($name, $href, $text, $js) {
161 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
165 * Create links that we can use in the form.
167 * @param CRM_Core_Form $form
169 * @param string $prefix
170 * The attribute that we are referencing.
171 * @param string $showLinkText
172 * The text to be shown for the show link.
173 * @param string $hideLinkText
174 * The text to be shown for the hide link.
176 * @param bool $assign
180 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
181 $showCode = "cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();";
182 $hideCode = "cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show(); return false;";
186 $values['show'] = self
::linkHtml("${prefix}_show", "#${prefix}_hide", self
::$_showIcon . $showLinkText, "onclick=\"$showCode\"");
187 $values['hide'] = self
::linkHtml("${prefix}_hide", "#${prefix}", self
::$_hideIcon . $hideLinkText, "onclick=\"$hideCode\"");
190 $form->assign($prefix, $values);
198 * Create html link elements that we can use in the form.
200 * @param CRM_Core_Form $form
203 * The current index of the element being processed.
204 * @param int $maxIndex
205 * The max number of elements that will be processed.
206 * @param string $prefix
207 * The attribute that we are referencing.
208 * @param string $showLinkText
209 * The text to be shown for the show link.
210 * @param string $hideLinkText
211 * The text to be shown for the hide link.
212 * @param string $elementType
214 * @param string $hideLink
215 * The hide block string.
217 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
218 $showHidePrefix = str_replace(array("]", "["), array("", "_"), $prefix);
219 $showHidePrefix = "id_" . $showHidePrefix;
220 if ($index == $maxIndex) {
221 $showCode = $hideCode = "return false;";
226 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
228 $hideCode = $hideLink;
231 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
235 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
236 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
242 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
243 array('onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode)
245 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
246 array('onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode)
250 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
251 array('onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode)
253 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
254 array('onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode)