PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/form/FormBuilderVocabulary.inc.php

https://github.com/jalilyacob/pkp-lib
PHP | 890 lines | 549 code | 106 blank | 235 comment | 60 complexity | fb12acd4143158c94e4a175684171cab MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @defgroup FormBuilderVocabulary
  4. */
  5. /**
  6. * @file classes/form/FormBuilderVocabulary.inc.php
  7. *
  8. * Copyright (c) 2000-2013 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class Fbv
  12. * @ingroup core
  13. *
  14. * @brief Class defining Form Builder Vocabulary methods.
  15. *
  16. * Form Builder Vocabulary - FBV
  17. * Generates form markup in templates using {fbvX} calls.
  18. * Group form areas with the {fbvFormArea} call. These sections mark off groups of semantically
  19. * related form sections.
  20. * Parameters:
  21. * id: The form area ID
  22. * class (optional): Any additional classes
  23. * title (optional): Title of the area
  24. * Group form sections with the {fbvFormSection} call. These sections organize directly related form elements.
  25. * Parameters:
  26. * id: The section ID
  27. * class (optional): Any additional classes
  28. * title (optional): Title of the area
  29. * Form submit/cancel buttons should be created with {fbvFormButtons}
  30. * Parameters:
  31. * submitText (optional): Text to display for the submit link (default is 'Ok')
  32. * submitDisabled (optional): Whether the submit button should be disabled
  33. * confirmSubmit (optional): Text to display in a confirmation dialog that must be okayed
  34. * before the form is submitted
  35. * cancelText (optional): Text to display for the cancel link (default is 'Cancel')
  36. * hideCancel (optional): Whether the submit button should be disabled
  37. * confirmCancel (optional): Text to display in cancel button's confirmation dialog
  38. * cancelAction (optional): A LinkAction object to execute when cancel is clicked
  39. * cancelUrl (optional): URL to redirect to when cancel is clicked
  40. * Form elements are created with {fbvElement type="type"} plus any additional parameters.
  41. * Each specific element type may have other additional attributes (see their method comments)
  42. * Parameters:
  43. * type: The form element type (one of the cases in the smartyFBVElement method)
  44. * id: The element ID
  45. * class (optional): Any additional classes
  46. * required (optional) whether the section should have a 'required' label (adds span.required)
  47. * for (optional): What the section's label is for
  48. * inline: Adds .inline to the element's parent container and causes it to display inline with other elements
  49. * size: One of $fbvStyles.size.SMALL (adds .quarter to element's parent container) or $fbvStyles.size.MEDIUM (adds
  50. * .half to element's parentcontainer)
  51. * required: Adds an asterisk and a .required class to the element's label
  52. */
  53. class FormBuilderVocabulary {
  54. /** Form associated with this object, if any. Will inform smarty which forms to label as required **/
  55. var $_form;
  56. /** Styles organized by parameter name */
  57. var $_fbvStyles;
  58. /**
  59. * Constructor.
  60. * @param $form object Form associated with this object
  61. */
  62. function FormBuilderVocabulary($form = null) {
  63. $this->_fbvStyles = array(
  64. 'size' => array('SMALL' => 'SMALL', 'MEDIUM' => 'MEDIUM', 'LARGE' => 'LARGE'),
  65. 'height' => array('SHORT' => 'SHORT', 'MEDIUM' => 'MEDIUM', 'TALL' => 'TALL')
  66. );
  67. }
  68. //
  69. // Setters and Getters
  70. //
  71. /**
  72. * Set the form
  73. * @param $form object
  74. */
  75. function setForm(&$form) {
  76. if (isset($form)) assert(is_a($form, 'Form'));
  77. $this->_form =& $form;
  78. }
  79. /**
  80. * Get the form
  81. * @return Object
  82. */
  83. function getForm() {
  84. return $this->_form;
  85. }
  86. /**
  87. * Get the form style constants
  88. * @return array
  89. */
  90. function getStyles() {
  91. return $this->_fbvStyles;
  92. }
  93. //
  94. // Public Methods
  95. //
  96. /**
  97. * A form area that contains form sections.
  98. * @param $params array
  99. * @param $content string
  100. * @param $smarty object
  101. * @param $repeat
  102. */
  103. function smartyFBVFormArea($params, $content, &$smarty, &$repeat) {
  104. assert(isset($params['id']));
  105. if (!$repeat) {
  106. $smarty->assign('FBV_class', isset($params['class']) ? $params['class'] : null);
  107. $smarty->assign('FBV_id', $params['id']);
  108. $smarty->assign('FBV_content', isset($content) ? $content : null);
  109. $smarty->assign('FBV_translate', isset($params['translate']) ? $params['translate'] : true);
  110. $smarty->assign('FBV_title', isset($params['title']) ? $params['title'] : null);
  111. return $smarty->fetch('form/formArea.tpl');
  112. }
  113. return '';
  114. }
  115. /**
  116. * A form section that contains controls in a variety of layout possibilities.
  117. * @param $params array
  118. * @param $content string
  119. * @param $smarty object
  120. * @param $repeat
  121. */
  122. function smartyFBVFormSection($params, $content, &$smarty, &$repeat) {
  123. $form = $this->getForm();
  124. if (!$repeat) {
  125. $smarty->assign('FBV_required', isset($params['required']) ? $params['required'] : false);
  126. $smarty->assign('FBV_id', isset($params['id']) ? $params['id'] : null);
  127. // Since $content will contain input fields that may have unique Ids appended, the 'for'
  128. // attribute on the form section's label needs to include this. Look for the assigned
  129. // form element within $content and extract the full id. Default to the passed in param
  130. // otherwise.
  131. if (!empty($params['for'])) {
  132. if (preg_match('/id="(' . preg_quote($params['for'], '/') . '\-[^"]+)"/', $content, $matches)) {
  133. $smarty->assign('FBV_labelFor', $matches[1]);
  134. } else {
  135. $smarty->assign('FBV_labelFor', $params['for']);
  136. }
  137. } else {
  138. $smarty->assign('FBV_labelFor', null);
  139. }
  140. $smarty->assign('FBV_title', isset($params['title']) ? $params['title'] : null);
  141. $smarty->assign('FBV_label', isset($params['label']) ? $params['label'] : null);
  142. $smarty->assign('FBV_layoutInfo', $this->_getLayoutInfo($params));
  143. $smarty->assign('FBV_description', isset($params['description']) ? $params['description'] : null);
  144. $smarty->assign('FBV_content', isset($content) ? $content: null);
  145. // default is to perform translation:
  146. $smarty->assign('FBV_translate', isset($params['translate']) ? $params['translate'] : true);
  147. $class = $params['class'];
  148. // Check if we are using the Form class and if there are any errors
  149. $smarty->clear_assign(array('FBV_sectionErrors'));
  150. if (isset($form) && !empty($form->formSectionErrors)) {
  151. $class = $class . (empty($class) ? '' : ' ') . 'error';
  152. $smarty->assign('FBV_sectionErrors', $form->formSectionErrors);
  153. $form->formSectionErrors = array();
  154. }
  155. // If we are displaying checkboxes or radio options, we'll need to use a
  156. // list to organize our elements -- Otherwise we use divs and spans
  157. if (isset($params['list']) && $params['list'] != false) {
  158. $smarty->assign('FBV_listSection', true);
  159. } else {
  160. // Double check that we don't have lists in the content.
  161. // This is a kludge but the only way to make sure we've
  162. // set the list parameter when we're using lists
  163. if (substr(trim($content), 0, 4) == "<li>") {
  164. $smarty->trigger_error('FBV: list attribute not set on form section containing lists');
  165. }
  166. $smarty->assign('FBV_listSection', false);
  167. }
  168. $smarty->assign('FBV_class', $class);
  169. $smarty->assign('FBV_layoutColumns', empty($params['layout']) ? false : true);
  170. return $smarty->fetch('form/formSection.tpl');
  171. } else {
  172. if (isset($form)) $form->formSectionErrors = array();
  173. }
  174. return '';
  175. }
  176. /**
  177. * Submit and (optional) cancel button for a form.
  178. * @param $params array
  179. * @param $smarty object
  180. * @param $repeat
  181. */
  182. function smartyFBVFormButtons($params, &$smarty) {
  183. // Submit button options.
  184. $smarty->assign('FBV_submitText', isset($params['submitText']) ? $params['submitText'] : 'common.ok');
  185. $smarty->assign('FBV_submitDisabled', isset($params['submitDisabled']) ? (boolean)$params['submitDisabled'] : false);
  186. $smarty->assign('FBV_confirmSubmit', isset($params['confirmSubmit']) ? $params['confirmSubmit'] : null);
  187. // Cancel button options.
  188. $smarty->assign('FBV_cancelText', isset($params['cancelText']) ? $params['cancelText'] : 'common.cancel');
  189. $smarty->assign('FBV_hideCancel', isset($params['hideCancel']) ? (boolean)$params['hideCancel'] : false);
  190. $smarty->assign('FBV_confirmCancel', isset($params['confirmCancel']) ? $params['confirmCancel'] : null);
  191. $smarty->assign('FBV_cancelAction', isset($params['cancelAction']) ? $params['cancelAction'] : null);
  192. $smarty->assign('FBV_cancelUrl', isset($params['cancelUrl']) ? $params['cancelUrl'] : null);
  193. $smarty->assign('FBV_formReset', isset($params['formReset']) ? (boolean)$params['formReset'] : false);
  194. $smarty->assign('FBV_translate', isset($params['translate']) ? $params['translate'] : true);
  195. return $smarty->fetch('form/formButtons.tpl');
  196. }
  197. /**
  198. * Base form element.
  199. * @param $params array
  200. * @param $smarty object-
  201. */
  202. function smartyFBVElement($params, &$smarty, $content = null) {
  203. if (!isset($params['type'])) $smarty->trigger_error('FBV: Element type not set');
  204. if (!isset($params['id'])) $smarty->trigger_error('FBV: Element ID not set');
  205. // Set up the element template
  206. $smarty->assign('FBV_id', $params['id']);
  207. $smarty->assign('FBV_class', isset($params['class']) ? $params['class'] : null);
  208. $smarty->assign('FBV_required', isset($params['required']) ? $params['required'] : false);
  209. $smarty->assign('FBV_layoutInfo', $this->_getLayoutInfo($params));
  210. $smarty->assign('FBV_label', isset($params['label']) ? $params['label'] : null);
  211. $smarty->assign('FBV_for', isset($params['for']) ? $params['for'] : null);
  212. $smarty->assign('FBV_tabIndex', isset($params['tabIndex']) ? $params['tabIndex'] : null);
  213. $smarty->assign('FBV_translate', isset($params['translate']) ? $params['translate'] : true);
  214. $smarty->assign('FBV_keepLabelHtml', isset($params['keepLabelHtml']) ? $params['keepLabelHtml'] : false);
  215. // Unset these parameters so they don't get assigned twice
  216. unset($params['class']);
  217. // Find fields that the form class has marked as required and add the 'required' class to them
  218. $params = $this->_addClientSideValidation($params);
  219. $smarty->assign('FBV_validation', isset($params['validation']) ? $params['validation'] : null);
  220. // Set up the specific field's template
  221. switch (strtolower_codesafe($params['type'])) {
  222. case 'autocomplete':
  223. $content = $this->_smartyFBVAutocompleteInput($params, $smarty);
  224. break;
  225. case 'button':
  226. case 'submit':
  227. $content = $this->_smartyFBVButton($params, $smarty);
  228. break;
  229. case 'checkbox':
  230. $content = $this->_smartyFBVCheckbox($params, $smarty);
  231. unset($params['label']);
  232. break;
  233. case 'checkboxgroup':
  234. $content = $this->_smartyFBVCheckboxGroup($params, $smarty);
  235. unset($params['label']);
  236. break;
  237. case 'file':
  238. $content = $this->_smartyFBVFileInput($params, $smarty);
  239. break;
  240. case 'hidden':
  241. $content = $this->_smartyFBVHiddenInput($params, $smarty);
  242. break;
  243. case 'keyword':
  244. $content = $this->_smartyFBVKeywordInput($params, $smarty);
  245. break;
  246. case 'interests':
  247. $content = $this->_smartyFBVInterestsInput($params, $smarty);
  248. break;
  249. case 'link':
  250. $content = $this->_smartyFBVLink($params, $smarty);
  251. break;
  252. case 'radio':
  253. $content = $this->_smartyFBVRadioButton($params, $smarty);
  254. unset($params['label']);
  255. break;
  256. case 'rangeslider':
  257. $content = $this->_smartyFBVRangeSlider($params, $smarty);
  258. break;
  259. case 'select':
  260. $content = $this->_smartyFBVSelect($params, $smarty);
  261. break;
  262. case 'text':
  263. $content = $this->_smartyFBVTextInput($params, $smarty);
  264. break;
  265. case 'textarea':
  266. $content = $this->_smartyFBVTextArea($params, $smarty);
  267. break;
  268. default: assert(false);
  269. }
  270. unset($params['type']);
  271. $parent = $smarty->_tag_stack[count($smarty->_tag_stack)-1];
  272. $group = false;
  273. if ($parent) {
  274. $form = $this->getForm();
  275. if (isset($form) && isset($form->errorFields[$params['id']])) {
  276. array_push($form->formSectionErrors, $form->errorsArray[$params['id']]);
  277. }
  278. if (isset($parent[1]['group']) && $parent[1]['group']) {
  279. $group = true;
  280. }
  281. }
  282. return $content;
  283. }
  284. //
  285. // Private methods
  286. //
  287. /**
  288. * Form button.
  289. * parameters: label (or value), disabled (optional), type (optional)
  290. * @param $params array
  291. * @param $smarty object
  292. */
  293. function _smartyFBVButton($params, &$smarty) {
  294. // the type of this button. the default value is 'button' (but could be 'submit')
  295. $buttonParams = '';
  296. $smarty->clear_assign(array('FBV_label', 'FBV_disabled'));
  297. foreach ($params as $key => $value) {
  298. switch ($key) {
  299. case 'inline':
  300. break;
  301. case 'label':
  302. case 'type':
  303. case 'disabled':
  304. $smarty->assign('FBV_' . $key, $value);
  305. break;
  306. default: $buttonParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  307. }
  308. }
  309. $smarty->assign('FBV_buttonParams', $buttonParams);
  310. return $smarty->fetch('form/button.tpl');
  311. }
  312. /**
  313. * Text link.
  314. * parameters: label (or value), disabled (optional)
  315. * @param $params array
  316. * @param $smarty object
  317. */
  318. function _smartyFBVLink($params, &$smarty) {
  319. assert(isset($params['id']));
  320. // Set the URL if there is one (defaults to '#' e.g. when the link should activate javascript)
  321. $smarty->assign('FBV_href', isset($params['href']) ? $params['href'] : '#');
  322. $smarty->clear_assign(array('FBV_label', 'FBV_disabled'));
  323. foreach ($params as $key => $value) {
  324. switch ($key) {
  325. case 'label':
  326. case 'disabled':
  327. $smarty->assign('FBV_' . $key, $value);
  328. break;
  329. }
  330. }
  331. return $smarty->fetch('form/link.tpl');
  332. }
  333. /**
  334. * Form Autocomplete text input. (actually two inputs, label and value)
  335. * parameters: disabled (optional), name (optional - assigned value of 'id' by default)
  336. * @param $params array
  337. * @param $smarty object
  338. */
  339. function _smartyFBVAutocompleteInput($params, &$smarty) {
  340. assert(isset($params['autocompleteUrl']) && isset($params['id']));
  341. // This id will be used for the hidden input that should be read by the Form.
  342. $autocompleteId = $params['id'];
  343. // We then override the id parameter to differentiate it from the hidden element
  344. // and make sure that the text input is not read by the Form class.
  345. $params['id'] = $autocompleteId . '_input';
  346. $smarty->clear_assign(array('FBV_id', 'FBV_autocompleteUrl', 'FBV_autocompleteValue'));
  347. // We set this now, so that we unset the param for the text input.
  348. $smarty->assign('FBV_autocompleteUrl', $params['autocompleteUrl']);
  349. $smarty->assign('FBV_autocompleteValue', isset($params['autocompleteValue']) ? $params['autocompleteValue'] : null);
  350. $smarty->assign('FBV_disableSync', isset($params['disableSync']) ? true : null);
  351. unset($params['autocompleteUrl']);
  352. unset($params['autocompleteValue']);
  353. $smarty->assign('FBV_textInput', $this->_smartyFBVTextInput($params, $smarty));
  354. $smarty->assign('FBV_id', $autocompleteId);
  355. return $smarty->fetch('form/autocompleteInput.tpl');
  356. }
  357. /**
  358. * Range slider input.
  359. * parameters: min, max
  360. * @param $params array
  361. * @param $smarty object
  362. */
  363. function _smartyFBVRangeSlider($params, &$smarty) {
  364. // Make sure our required fields are included
  365. assert(isset($params['min']) && isset($params['max']));
  366. // Assign the min and max values to the handler
  367. $smarty->assign('FBV_min', $params['min']);
  368. $smarty->assign('FBV_max', $params['max']);
  369. $smarty->assign('FBV_label_content', isset($params['label']) ? $this->_smartyFBVSubLabel($params, $smarty) : null);
  370. return $smarty->fetch('form/rangeSlider.tpl');
  371. }
  372. /**
  373. * Form text input.
  374. * parameters: disabled (optional), name (optional - assigned value of 'id' by default), multilingual (optional)
  375. * @param $params array
  376. * @param $smarty object
  377. */
  378. function _smartyFBVTextInput($params, &$smarty) {
  379. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  380. $params['subLabelTranslate'] = isset($params['subLabelTranslate']) ? (boolean) $params['subLabelTranslate'] : true;
  381. $params['uniqId'] = uniqid();
  382. $smarty->assign('FBV_isPassword', isset($params['password']) ? true : false);
  383. $textInputParams = '';
  384. $smarty->clear_assign(array('FBV_disabled', 'FBV_readonly', 'FBV_multilingual', 'FBV_name', 'FBV_value', 'FBV_label_content', 'FBV_uniqId'));
  385. foreach ($params as $key => $value) {
  386. switch ($key) {
  387. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  388. case 'type': break;
  389. case 'size': break;
  390. case 'inline': break;
  391. case 'subLabelTranslate': break;
  392. case 'disabled':
  393. case 'readonly':
  394. case 'multilingual':
  395. case 'name':
  396. case 'id':
  397. case 'value':
  398. case 'uniqId':
  399. $smarty->assign('FBV_' . $key, $value); break;
  400. break;
  401. case 'required': if ($value != 'true') $textInputParams .= 'required="' + htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) +'"'; break;
  402. default: $textInputParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING). '" ';
  403. }
  404. }
  405. $smarty->assign('FBV_textInputParams', $textInputParams);
  406. return $smarty->fetch('form/textInput.tpl');
  407. }
  408. /**
  409. * Form text area.
  410. * parameters: value, id, name (optional - assigned value of 'id' by default), disabled (optional), multilingual (optional)
  411. * @param $params array
  412. * @param $smarty object
  413. */
  414. function _smartyFBVTextArea($params, &$smarty) {
  415. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  416. $params['rows'] = isset($params['rows']) ? $params['rows'] : 10;
  417. $params['cols'] = isset($params['cols']) ? $params['cols'] : 80;
  418. $params['subLabelTranslate'] = isset($params['subLabelTranslate']) ? (boolean) $params['subLabelTranslate'] : true;
  419. $params['uniqId'] = uniqid();
  420. $textAreaParams = '';
  421. $smarty->clear_assign(array('FBV_label_content', 'FBV_disabled', 'FBV_readonly', 'FBV_multilingual', 'FBV_name', 'FBV_value', 'FBV_height', 'FBV_uniqId', 'FBV_rows', 'FBV_cols'));
  422. foreach ($params as $key => $value) {
  423. switch ($key) {
  424. case 'name':
  425. case 'value':
  426. case 'rows':
  427. case 'cols':
  428. case 'rich':
  429. case 'disabled':
  430. case 'readonly':
  431. case 'multilingual':
  432. case 'uniqId':
  433. $smarty->assign('FBV_' . $key, $value);
  434. break;
  435. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  436. case 'type': break;
  437. case 'size': break;
  438. case 'inline': break;
  439. case 'subLabelTranslate': break;
  440. case 'height':
  441. $styles = $this->getStyles();
  442. switch($value) {
  443. case $styles['height']['SHORT']: $smarty->assign('FBV_height', 'short'); break;
  444. case $styles['height']['MEDIUM']: $smarty->assign('FBV_height', 'medium'); break;
  445. case $styles['height']['TALL']: $smarty->assign('FBV_height', 'tall'); break;
  446. default:
  447. $smarty->trigger_error('FBV: invalid height specified for textarea.');
  448. }
  449. break;
  450. case 'id': break; // if we don't do this, the textarea ends up with two id attributes because FBV_id is also set.
  451. default: $textAreaParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  452. }
  453. }
  454. $smarty->assign('FBV_textAreaParams', $textAreaParams);
  455. return $smarty->fetch('form/textarea.tpl');
  456. }
  457. /**
  458. * Hidden input element.
  459. * parameters: value, id, name (optional - assigned value of 'id' by default), disabled (optional), multilingual (optional)
  460. * @param $params array
  461. * @param $smarty object
  462. */
  463. function _smartyFBVHiddenInput($params, &$smarty) {
  464. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  465. $hiddenInputParams = '';
  466. $smarty->clear_assign(array('FBV_id', 'FBV_value'));
  467. foreach ($params as $key => $value) {
  468. switch ($key) {
  469. case 'name':
  470. case 'id':
  471. case 'value':
  472. $smarty->assign('FBV_' . $key, $value);
  473. break;
  474. case 'label': break;
  475. case 'type': break;
  476. default: $hiddenInputParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  477. }
  478. }
  479. $smarty->assign('FBV_hiddenInputParams', $hiddenInputParams);
  480. return $smarty->fetch('form/hiddenInput.tpl');
  481. }
  482. /**
  483. * Form select control.
  484. * parameters: from [array], selected [array index], defaultLabel (optional), defaultValue (optional), disabled (optional),
  485. * translate (optional), name (optional - value of 'id' by default)
  486. * @param $params array
  487. * @param $smarty object
  488. */
  489. function _smartyFBVSelect($params, &$smarty) {
  490. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  491. $params['translate'] = isset($params['translate']) ? $params['translate'] : true;
  492. $params['subLabelTranslate'] = isset($params['subLabelTranslate']) ? (boolean) $params['subLabelTranslate'] : true;
  493. $selectParams = '';
  494. $smarty->clear_assign(array('FBV_from', 'FBV_selected', 'FBV_label_content', 'FBV_defaultValue', 'FBV_defaultLabel'));
  495. foreach ($params as $key => $value) {
  496. switch ($key) {
  497. case 'from':
  498. case 'selected':
  499. case 'translate':
  500. case 'defaultValue':
  501. case 'defaultLabel':
  502. case 'disabled':
  503. $smarty->assign('FBV_' . $key, $value);
  504. break;
  505. case 'type':
  506. case 'inline':
  507. case 'size':
  508. break;
  509. case 'subLabelTranslate': break;
  510. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  511. default: $selectParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  512. }
  513. }
  514. $smarty->assign('FBV_selectParams', $selectParams);
  515. return $smarty->fetch('form/select.tpl');
  516. }
  517. /**
  518. * Form checkbox group control.
  519. * parameters: from [array], selected [array index], defaultLabel (optional), defaultValue (optional), disabled (optional),
  520. * translate (optional), name (optional - value of 'id' by default)
  521. * @param $params array
  522. * @param $smarty object
  523. */
  524. function _smartyFBVCheckboxGroup($params, &$smarty) {
  525. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  526. $params['translate'] = isset($params['translate']) ? (boolean)$params['translate'] : true;
  527. $params['subLabelTranslate'] = isset($params['subLabelTranslate']) ? (boolean) $params['subLabelTranslate'] : true;
  528. $checkboxParams = '';
  529. $smarty->clear_assign(array('FBV_from', 'FBV_selected', 'FBV_label_content', 'FBV_defaultValue', 'FBV_defaultLabel', 'FBV_name'));
  530. foreach ($params as $key => $value) {
  531. switch ($key) {
  532. case 'from':
  533. case 'selected':
  534. case 'defaultValue':
  535. case 'defaultLabel':
  536. case 'translate':
  537. case 'name':
  538. case 'validation':
  539. case 'disabled':
  540. $smarty->assign('FBV_' . $key, $value);
  541. break;
  542. case 'type': break;
  543. case 'inline': break;
  544. case 'subLabelTranslate': break;
  545. default: $checkboxParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  546. }
  547. }
  548. $smarty->assign('FBV_checkboxParams', $checkboxParams);
  549. return $smarty->fetch('form/checkboxGroup.tpl');
  550. }
  551. /**
  552. * Checkbox input control.
  553. * parameters: label, disabled (optional), translate (optional), name (optional - value of 'id' by default)
  554. * @param $params array
  555. * @param $smarty object
  556. */
  557. function _smartyFBVCheckbox($params, &$smarty) {
  558. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  559. $params['translate'] = isset($params['translate']) ? (boolean)$params['translate'] : true;
  560. $checkboxParams = '';
  561. $smarty->clear_assign(array('FBV_id', 'FBV_label', 'FBV_checked', 'FBV_disabled'));
  562. foreach ($params as $key => $value) {
  563. switch ($key) {
  564. case 'type': break;
  565. case 'id':
  566. case 'label':
  567. case 'translate':
  568. case 'checked':
  569. case 'disabled':
  570. $smarty->assign('FBV_' . $key, $value);
  571. break;
  572. default: $checkboxParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  573. }
  574. }
  575. $smarty->assign('FBV_checkboxParams', $checkboxParams);
  576. return $smarty->fetch('form/checkbox.tpl');
  577. }
  578. /**
  579. * Radio input control.
  580. * parameters: label, disabled (optional), translate (optional), name (optional - value of 'id' by default)
  581. * @param $params array
  582. * @param $smarty object
  583. */
  584. function _smartyFBVRadioButton($params, &$smarty) {
  585. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  586. $params['translate'] = isset($params['translate']) ? $params['translate'] : true;
  587. if (isset($params['label']) && isset($params['content'])) {
  588. $smarty->trigger_error('FBV: radio button cannot have both a content and a label parameter. Label has precedence.');
  589. }
  590. $radioParams = '';
  591. $smarty->clear_assign(array('FBV_id', 'FBV_label', 'FBV_content', 'FBV_checked', 'FBV_disabled'));
  592. foreach ($params as $key => $value) {
  593. switch ($key) {
  594. case 'type': break;
  595. case 'id':
  596. case 'label':
  597. case 'translate':
  598. case 'checked':
  599. case 'disabled':
  600. case 'content':
  601. $smarty->assign('FBV_' . $key, $value);
  602. break;
  603. default: $radioParams .= htmlspecialchars($key, ENT_QUOTES, LOCALE_ENCODING) . '="' . htmlspecialchars($value, ENT_QUOTES, LOCALE_ENCODING) . '" ';
  604. }
  605. }
  606. $smarty->assign('FBV_radioParams', $radioParams);
  607. return $smarty->fetch('form/radioButton.tpl');
  608. }
  609. /**
  610. * File upload input.
  611. * parameters: submit (optional - name of submit button to include), disabled (optional), name (optional - value of 'id' by default)
  612. * @param $params array
  613. * @param $smarty object
  614. */
  615. function _smartyFBVFileInput($params, &$smarty) {
  616. $params['name'] = isset($params['name']) ? $params['name'] : $params['id'];
  617. $params['translate'] = isset($params['translate']) ? $params['translate'] : true;
  618. $smarty->clear_assign(array('FBV_id', 'FBV_label_content', 'FBV_checked', 'FBV_disabled', 'FBV_submit'));
  619. foreach ($params as $key => $value) {
  620. switch ($key) {
  621. case 'type': break;
  622. case 'id':
  623. case 'submit':
  624. case 'name':
  625. case 'disabled':
  626. $smarty->assign('FBV_' . $key, $value);
  627. break;
  628. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  629. }
  630. }
  631. return $smarty->fetch('form/fileInput.tpl');
  632. }
  633. /**
  634. * Keyword input.
  635. * parameters: available - all available keywords (for autosuggest); current - user's current keywords
  636. * @param $params array
  637. * @param $smarty object
  638. */
  639. function _smartyFBVKeywordInput($params, &$smarty) {
  640. $params['uniqId'] = uniqid();
  641. $smarty->clear_assign(array('FBV_id', 'FBV_label', 'FBV_availableKeywords', 'FBV_currentKeywords', 'FBV_multilingual', 'FBV_sourceUrl', 'FBV_uniqId', 'FBV_disabled'));
  642. foreach ($params as $key => $value) {
  643. switch ($key) {
  644. case 'type': break;
  645. case 'id':
  646. case 'uniqId':
  647. case 'disabled':
  648. case 'multilingual':
  649. $smarty->assign('FBV_' . $key, $value);
  650. break;
  651. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  652. case 'available': $smarty->assign('FBV_availableKeywords', $value); break;
  653. case 'current': $smarty->assign('FBV_currentKeywords', $value); break;
  654. case 'source': $smarty->assign('FBV_sourceUrl', $value); break;
  655. }
  656. }
  657. return $smarty->fetch('form/keywordInput.tpl');
  658. }
  659. /**
  660. * Reviewing interests input.
  661. * parameters: interestsKeywords - current users's keywords (array); interestsTextOnly - user's current keywords (comma separated string)
  662. * @param $params array
  663. * @param $smarty object
  664. */
  665. function _smartyFBVInterestsInput($params, &$smarty) {
  666. $smarty->clear_assign(array('FBV_id', 'FBV_label', 'FBV_label_content', 'FBV_interestsKeywords', 'FBV_interestsTextOnly'));
  667. foreach ($params as $key => $value) {
  668. switch ($key) {
  669. case 'type': break;
  670. case 'id':
  671. case 'interestsKeywords':
  672. case 'interestsTextOnly':
  673. $smarty->assign('FBV_' . $key, $value);
  674. break;
  675. case 'label': $smarty->assign('FBV_label_content', $this->_smartyFBVSubLabel($params, $smarty)); break;
  676. }
  677. }
  678. return $smarty->fetch('form/interestsInput.tpl');
  679. }
  680. /**
  681. * Custom Smarty function for labelling/highlighting of form fields.
  682. * @param $params array can contain 'name' (field name/ID), 'required' (required field), 'key' (localization key), 'label' (non-localized label string), 'suppressId' (boolean)
  683. * @param $smarty Smarty
  684. */
  685. function _smartyFBVSubLabel($params, &$smarty) {
  686. assert(isset($params['label']));
  687. $returner = '';
  688. $form = $this->getForm();
  689. if (isset($form) && isset($form->errorFields[$params['name']])) {
  690. $smarty->assign('FBV_error', true);
  691. } else {
  692. $smarty->assign('FBV_error', false);
  693. }
  694. $smarty->clear_assign(array('FBV_suppressId', 'FBV_label', 'FBV_required', 'FBV_uniqId', 'FBV_multilingual', 'FBV_required'));
  695. foreach ($params as $key => $value) {
  696. switch ($key) {
  697. case 'subLabelTranslate': $smarty->assign('FBV_subLabelTranslate', $value); break;
  698. case 'label':
  699. case 'uniqId':
  700. case 'multilingual':
  701. case 'suppressId':
  702. case 'required':
  703. $smarty->assign('FBV_' . $key, $value);
  704. break;
  705. }
  706. }
  707. $returner = $smarty->fetch('form/subLabel.tpl');
  708. return $returner;
  709. }
  710. /**
  711. * Assign the appropriate class name to the element for client-side validation
  712. * @param $params array
  713. * return array
  714. */
  715. function _addClientSideValidation($params) {
  716. $form = $this->getForm();
  717. if (isset($form)) {
  718. // Assign the appropriate class name to the element for client-side validation
  719. $fieldId = $params['id'];
  720. if (isset($form->cssValidation[$fieldId])) {
  721. $params['validation'] = implode(' ', $form->cssValidation[$fieldId]);
  722. }
  723. }
  724. return $params;
  725. }
  726. /**
  727. * Cycle through layout parameters to add the appropriate classes to the element's parent container
  728. * @param $params array
  729. * @return string
  730. */
  731. function _getLayoutInfo($params) {
  732. $classes = array();
  733. foreach ($params as $key => $value) {
  734. switch ($key) {
  735. case 'size':
  736. switch($value) {
  737. case 'SMALL': $classes[] = 'pkp_helpers_quarter'; break;
  738. case 'MEDIUM': $classes[] = 'pkp_helpers_half'; break;
  739. CASE 'LARGE': $classes[] = 'pkp_helpers_threeQuarter'; break;
  740. }
  741. break;
  742. case 'inline':
  743. if($value) $classes[] = 'inline'; break;
  744. }
  745. }
  746. if(!empty($classes)) {
  747. return implode(' ', $classes);
  748. } else return null;
  749. }
  750. /**
  751. * Custom Smarty function for labelling/highlighting of form fields.
  752. * @param $params array can contain 'name' (field name/ID), 'required' (required field), 'key' (localization key), 'label' (non-localized label string), 'suppressId' (boolean)
  753. * @param $smarty Smarty
  754. */
  755. function smartyFieldLabel($params, &$smarty) {
  756. $returner = '';
  757. if (isset($params) && !empty($params)) {
  758. if (isset($params['key'])) {
  759. $params['label'] = __($params['key'], $params);
  760. }
  761. $form = $this->getForm();
  762. if (isset($form) && isset($form->errorFields[$params['name']])) {
  763. $smarty->assign('FBV_class', 'error ' . $params['class']);
  764. } else {
  765. $smarty->assign('FBV_class', $params['class']);
  766. }
  767. $smarty->clear_assign(array('FBV_suppressId', 'FBV_label', 'FBV_required', 'FBV_disabled', 'FBV_name'));
  768. foreach ($params as $key => $value) {
  769. switch ($key) {
  770. case 'label':
  771. case 'required':
  772. case 'suppressId':
  773. case 'disabled':
  774. case 'name':
  775. $smarty->assign('FBV_' . $key, $value);
  776. break;
  777. }
  778. }
  779. $returner = $smarty->fetch('form/fieldLabel.tpl');
  780. }
  781. return $returner;
  782. }
  783. }
  784. ?>