PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/media/zoo/elements/option/option.php

https://bitbucket.org/kraymitchell/saiu
PHP | 154 lines | 54 code | 16 blank | 84 comment | 3 complexity | b9595293d40066a35f5076e9f1018935 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: ElementOption
  10. The option elements base class
  11. */
  12. abstract class ElementOption extends Element implements iSubmittable {
  13. /*
  14. Function: hasValue
  15. Checks if the element's value is set.
  16. Parameters:
  17. $params - render parameter
  18. Returns:
  19. Boolean - true, on success
  20. */
  21. public function hasValue($params = array()) {
  22. foreach ($this->get('option', array()) as $option) {
  23. if (!empty($option)) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. }
  29. /*
  30. Function: render
  31. Renders the element.
  32. Parameters:
  33. $params - render parameter
  34. Returns:
  35. String - html
  36. */
  37. public function render($params = array()) {
  38. // init vars
  39. $params = $this->app->data->create($params);
  40. $selected_options = $this->get('option', array());
  41. $options = array();
  42. foreach ($this->config->get('option', array()) as $option) {
  43. if (in_array($option['value'], $selected_options)) {
  44. $options[] = $option['name'];
  45. }
  46. }
  47. return $this->app->element->applySeparators($params->get('separated_by'), $options);
  48. }
  49. /*
  50. Function: getSearchData
  51. Get elements search data.
  52. Returns:
  53. String - Search data
  54. */
  55. public function getSearchData() {
  56. $options = $this->get('option', array());
  57. $result = array();
  58. foreach ($this->config->get('option', array()) as $option) {
  59. if (in_array($option['value'], $options)) {
  60. $result[] = $option['name'];
  61. }
  62. }
  63. return (empty($result) ? null : implode("\n", $result));
  64. }
  65. /*
  66. Function: editOption
  67. Renders elements options for form input.
  68. Parameters:
  69. $var - form var name
  70. $num - option order number
  71. Returns:
  72. Array
  73. */
  74. public function editOption($var, $num, $name = null, $value = null){
  75. return $this->renderLayout($this->app->path->path("elements:option/tmpl/editoption.php"), compact('var', 'num', 'name', 'value'));
  76. }
  77. /*
  78. Function: getConfigForm
  79. Get parameter form object to render input form.
  80. Returns:
  81. Parameter Object
  82. */
  83. public function getConfigForm() {
  84. return parent::getConfigForm()->addElementPath(dirname(__FILE__));
  85. }
  86. /*
  87. Function: loadAssets
  88. Load elements css/js config assets.
  89. Returns:
  90. Void
  91. */
  92. public function loadConfigAssets() {
  93. $this->app->document->addScript('elements:option/option.js');
  94. $this->app->document->addStylesheet('elements:option/option.css');
  95. return parent::loadConfigAssets();
  96. }
  97. /*
  98. Function: renderSubmission
  99. Renders the element in submission.
  100. Parameters:
  101. $params - AppData submission parameters
  102. Returns:
  103. String - html
  104. */
  105. public function renderSubmission($params = array()) {
  106. return $this->edit();
  107. }
  108. /*
  109. Function: validateSubmission
  110. Validates the submitted element
  111. Parameters:
  112. $value - AppData value
  113. $params - AppData submission parameters
  114. Returns:
  115. Array - cleaned value
  116. */
  117. public function validateSubmission($value, $params) {
  118. $options = array('required' => $params->get('required'));
  119. $messages = array('required' => 'Please choose an option.');
  120. $option = $this->app->validator
  121. ->create('foreach', $this->app->validator->create('string', $options, $messages), $options, $messages)
  122. ->clean($value->get('option'));
  123. return compact('option');
  124. }
  125. }