PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Adapto/Attribute/FieldSet.php

http://github.com/egeniq/adapto
PHP | 205 lines | 85 code | 28 blank | 92 comment | 9 complexity | 6a63c314cbd14e441a5db986964c1d2d MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package adapto
  9. * @subpackage attributes
  10. *
  11. * @copyright (c) 2000-2008 Ibuildings.nl BV
  12. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  13. *
  14. */
  15. /**
  16. * A fieldset can be used to combine multiple attributes to a single
  17. * attribute in edit/view mode.
  18. *
  19. * @author petercv
  20. * @package adapto
  21. * @subpackage attributes
  22. */
  23. class Adapto_Attribute_FieldSet extends Adapto_Attribute
  24. {
  25. private $m_template;
  26. private $m_parser;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name fieldset name
  31. * @param string $template template string
  32. * @param int $flags flags
  33. */
  34. public function __construct($name, $template, $flags = 0)
  35. {
  36. parent::__construct($name, $flags | AF_NO_SORT);
  37. $this->setTemplate($template);
  38. $this->setLoadType(NOLOAD);
  39. $this->setStorageType(NOSTORE);
  40. }
  41. /**
  42. * Is empty?
  43. *
  44. * @return boolean
  45. */
  46. public function isEmpty()
  47. {
  48. // always return false, this way you can mark a field-set as obligatory
  49. // as a visual cue without ATK complaining that no value has been set
  50. return false;
  51. }
  52. /**
  53. * Check if one of the fields contains an error.
  54. *
  55. * @param array $errors The error list is one that is stored in the
  56. * "atkerror" section of a record, for example
  57. * generated by validate() methods.
  58. * @return boolean
  59. */
  60. function getError($errors)
  61. {
  62. $fields = array_unique($this->getParser()->getFields());
  63. foreach ($fields as $field) {
  64. @list($attrName) = explode('.', $field);
  65. $attr = $this->getOwnerInstance()->getAttribute($attrName);
  66. if ($attr->getError($errors)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * Returns the fieldset template.
  74. *
  75. * @return string template string
  76. */
  77. public function getTemplate()
  78. {
  79. return $this->m_template;
  80. }
  81. /**
  82. * Sets the fieldset template. To include an attribute label use
  83. * [attribute.label] to include an attribute edit/display field
  84. * use [attribute.field].
  85. *
  86. * @param string $template template string
  87. */
  88. public function setTemplate($template)
  89. {
  90. $this->m_template = $template;
  91. $this->m_parser = null;
  92. }
  93. /**
  94. * Returns the string parser instance for the fieldset template.
  95. *
  96. * @return atkStringParser
  97. */
  98. protected function getParser()
  99. {
  100. if ($this->m_parser == null) {
  101. $this->m_parser = new Adapto_StringParser($this->getTemplate());
  102. }
  103. return $this->m_parser;
  104. }
  105. /**
  106. * Make sure we disable the normal rendering for attributes that
  107. * are part of this fieldset.
  108. */
  109. public function postInit()
  110. {
  111. $fields = $this->getParser()->getFields();
  112. foreach ($fields as $field) {
  113. list($attrName) = explode('.', $field);
  114. $attr = $this->getOwnerInstance()->getAttribute($attrName);
  115. $attr->addDisabledMode(DISABLED_VIEW | DISABLED_EDIT);
  116. $attr->setTabs($this->getTabs());
  117. $attr->setSections($this->getSections());
  118. }
  119. }
  120. /**
  121. * Renders the fieldset.
  122. *
  123. * @param string $type edit or display
  124. * @param array $record record
  125. * @param string $mode mode
  126. * @param string $fieldprefix fieldprefix
  127. *
  128. * @return string rendered HTML
  129. */
  130. protected function renderFieldSet($type, $record, $mode, $fieldprefix = '')
  131. {
  132. $replacements = array();
  133. $fields = array_unique($this->getParser()->getFields());
  134. foreach ($fields as $field) {
  135. @list($attrName, $part) = explode('.', $field);
  136. $attr = $this->getOwnerInstance()->getAttribute($attrName);
  137. switch ($part) {
  138. case 'label':
  139. $replacements[$attrName][$part] = $attr->getLabel($record, $mode);
  140. break;
  141. case 'field':
  142. if ($type == 'edit')
  143. $replacements[$attrName][$part] = $attr->getEdit($mode, $record, $fieldprefix);
  144. else if ($type == 'display')
  145. $replacements[$attrName][$part] = $attr->display($record, $mode);
  146. break;
  147. }
  148. }
  149. return $this->getParser()->parse($replacements);
  150. }
  151. /**
  152. * Edit fieldset.
  153. *
  154. * @param array $record
  155. * @param string $fieldprefix
  156. * @param string $mode
  157. *
  158. * @return string
  159. */
  160. public function edit($record, $fieldprefix = '', $mode = '')
  161. {
  162. return $this->renderFieldSet('edit', $record, $mode, $fieldprefix);
  163. }
  164. /**
  165. * Display fieldset.
  166. *
  167. * @param string $record
  168. * @param string $mode
  169. *
  170. * @return string
  171. */
  172. public function display($record, $mode = '')
  173. {
  174. return $this->renderFieldSet('display', $record, $mode);
  175. }
  176. }