Source for file WOrderedRepeatableComponentCollection.class.php

Documentation is available at WOrderedRepeatableComponentCollection.class.php

  1. <?php
  2. /**
  3. * @since Aug 1, 2005
  4. * @package polyphony.wizard.components
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: WOrderedRepeatableComponentCollection.class.php,v 1.12 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. require_once(POLYPHONY."/main/library/Wizard/Components/WSelectList.class.php");
  13.  
  14. /**
  15. * This component allows for the creation of ordered repeatable components or groups of components.
  16. *
  17. * @since Aug 1, 2005
  18. * @package polyphony.wizard.components
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: WOrderedRepeatableComponentCollection.class.php,v 1.12 2007/09/19 14:04:51 adamfranco Exp $
  24. */
  25.  
  26. class WOrderedRepeatableComponentCollection
  27. extends WRepeatableComponentCollection
  28. {
  29.  
  30. var $_orderedSet;
  31. var $_nextId;
  32. function WOrderedRepeatableComponentCollection() {
  33. parent::WRepeatableComponentCollection();
  34. $idManager = Services::getService("Id");
  35. $this->_orderedSet = new OrderedSet($idManager->getId("unimportant"));
  36. $this->_nextId = 0;
  37. }
  38. /**
  39. * Adds a new element to the end of the list.
  40. * @param boolean $removable Can this collection be removed by the user?
  41. * @access private
  42. * @return void
  43. */
  44. function _addElement ($removable = true) {
  45. if ($this->_max != -1 && $this->_num == $this->_max - 1) return;
  46. // printDebugBacktrace();
  47. // clone our base set (the getChildren() array)
  48. $newArray = array();
  49. $base =$this->getChildren();
  50. foreach (array_keys($base) as $key) {
  51. $newArray[$key] =$base[$key]->copy();
  52. $newArray[$key]->setParent($this);
  53. }
  54. $newArray["_remove"] = WEventButton::withLabel($this->_removeLabel);
  55. $newArray["_remove"]->setParent($this);
  56. $newArray["_remove"]->addOnClick("ignoreValidation(this.form);");
  57. $newArray["_remove"]->setEnabled($removable, !$removable);
  58. $this->_collections[$this->_nextId] =$newArray;
  59. $idManager = Services::getService("Id");
  60. $this->_orderedSet->addItem($idManager->getId(strval($this->_nextId)));
  61. $this->_nextId++;
  62. $this->_num++;
  63. $this->rebuildPositionSelects();
  64. return $newArray;
  65. }
  66. /**
  67. * Removes the elements from our list.
  68. * @param array $ar An array of element keys.
  69. * @access private
  70. * @return void
  71. */
  72. function _removeElements ($ar) {
  73. if (($this->_num-count($ar)) < $this->_min) return;
  74. foreach ($ar as $key) {
  75. unset($this->_collections[$key]);
  76. $idManager = Services::getService("Id");
  77. $this->_orderedSet->removeItem($idManager->getId(strval($key)));
  78. $this->_num--;
  79. }
  80. }
  81. /**
  82. * Rebuild our position selects
  83. *
  84. * @return void
  85. * @access public
  86. * @since 5/16/06
  87. */
  88. function rebuildPositionSelects () {
  89. // Populate our position list;
  90. $positionList = new WSelectList;
  91. $js = '
  92. var choiceName = this.name + \'Choice\';
  93. for (var i = 0; i < this.form.elements.length; i++) {
  94. if (this.form.elements[i].name == choiceName) {
  95. this.form.elements[i].value = \'true\';
  96. break;
  97. }
  98. }
  99. this.form.submit();
  100. ';
  101. $positionList->addOnChange(preg_replace("/\s{2,}/", " ", preg_replace("/[\n\r\t]/", " ", $js)));
  102. $i = 0;
  103. $this->_orderedSet->reset();
  104. while ($this->_orderedSet->hasNext()) {
  105. $this->_orderedSet->next();
  106. $positionList->addOption($i, $i+1);
  107. $i++;
  108. }
  109. // Enabled State
  110. $positionList->setEnabled($this->_enabled, $this->_enabledSticky);
  111. // Rebuild the position lists.
  112. $this->_orderedSet->reset();
  113. while ($this->_orderedSet->hasNext()) {
  114. $collectionId =$this->_orderedSet->next();
  115. $key = $collectionId->getIdString();
  116. $this->_collections[$key]["_moveToPosition"] =$positionList->deepCopy();
  117. $this->_collections[$key]["_moveToPosition"]->setParent($this);
  118. $this->_collections[$key]["_moveToPosition"]->setValue(
  119. strval($this->_orderedSet->getPosition($collectionId)));
  120. $this->_collections[$key]["_moveToPositionChoice"] = WHiddenField::withValue('false');
  121. $this->_collections[$key]["_moveToPositionChoice"]->setParent($this);
  122. }
  123. }
  124. /**
  125. * Tells the wizard component to update itself - this may include getting
  126. * form post data or validation - whatever this particular component wants to
  127. * do every pageload.
  128. * @param string $fieldName The field name to use when outputting form data or
  129. * similar parameters/information.
  130. * @access public
  131. * @return boolean - TRUE if everything is OK
  132. */
  133. function update ($fieldName) {
  134. $idManager = Services::getService("Id");
  135. $ok = true;
  136. // $this->_removeElements(array(2));
  137. // first update all our components in the collections
  138. $toRemove = array();
  139. foreach(array_keys($this->_collections) as $key) {
  140. foreach(array_keys($this->_collections[$key]) as $name) {
  141. // print "$name in $key is a ".gettype($this->_collections[$key][$name])."<br/>";
  142. $rule = ExtendsValidatorRule::getRule("WizardComponent");
  143. if (!$rule->check($this->_collections[$key][$name])) continue;
  144. if (!$this->_collections[$key][$name]->update($fieldName."_".$key."_".$name))
  145. $ok = false;
  146. }
  147. if ($this->_collections[$key]["_remove"]->getAllValues())
  148. $toRemove[] = $key;
  149. $id = $idManager->getId(strval($key));
  150.  
  151. if (isset($this->_collections[$key]["_moveToPosition"])
  152. && isset($this->_collections[$key]["_moveToPositionChoice"])
  153. && $this->_collections[$key]["_moveToPositionChoice"]->getAllValues() == 'true')
  154. {
  155. $this->_orderedSet->moveToPosition($id,
  156. $this->_collections[$key]["_moveToPosition"]->getAllValues());
  157. }
  158. }
  159. $this->_removeElements($toRemove);
  160. // then, check if any "buttons" or anything were pressed to add/remove elements
  161. $this->_addButton->update($fieldName."_add");
  162. if ($this->_addButton->getAllValues()) {
  163. // print "adding element.<br/>";
  164. $this->_addElement();
  165. }
  166. $this->rebuildPositionSelects();
  167. return $ok;
  168. }
  169. /**
  170. * Sets if this component will be enabled or disabled.
  171. * @param boolean $enabled
  172. * @param boolean $sticky If true, future calls to setEnabled without sticky
  173. * will have no effect.
  174. * @access public
  175. * @return void
  176. */
  177. function setEnabled ($enabled, $sticky = false) {
  178. parent::setEnabled($enabled, $sticky);
  179. foreach ($this->_collections as $key => $copy) {
  180. $this->_collections[$key]["_moveToPositionChoice"]->setEnabled($enabled, $sticky);
  181. }
  182. }
  183. /**
  184. * Returns the values of wizard-components. Should return an array if children are involved,
  185. * otherwise a whatever type of object is expected.
  186. * @access public
  187. * @return mixed
  188. */
  189. function getAllValues () {
  190. // make an array indexed by collection of all the values.
  191. $array = array();
  192. $this->_orderedSet->reset();
  193. while ($this->_orderedSet->hasNext()) {
  194. $collectionId =$this->_orderedSet->next();
  195. $key = $collectionId->getIdString();
  196. foreach(array_keys($this->_collections[$key]) as $name) {
  197. $array[$key][$name] = $this->_collections[$key][$name]->getAllValues();
  198. }
  199. }
  200. return $array;
  201. }
  202. /**
  203. * Returns a block of XHTML-valid code that contains markup for this specific
  204. * component.
  205. * @param string $fieldName The field name to use when outputting form data or
  206. * similar parameters/information.
  207. * @access public
  208. * @return string
  209. */
  210. function getMarkup ($fieldName) {
  211. // check if we have min/max values that are appropriate, etc.
  212. if ($this->_num < $this->_min) $this->_num = $this->_min;
  213. if ($this->_max != -1 && $this->_num > $this->_max) $this->_num = $this->_max;
  214. $this->_ensureNumber($this->_num);
  215. $includeAdd = !($this->_num == $this->_max);
  216. $includeRemove = !($this->_num == $this->_min);
  217.  
  218. $m = "<table width='100%' border='0' cellspacing='0' cellpadding='2'>\n";
  219. $this->_orderedSet->reset();
  220. while ($this->_orderedSet->hasNext()) {
  221. $collectionId =$this->_orderedSet->next();
  222. $key = $collectionId->getIdString();
  223. $this->_collections[$key]["_remove"]->setEnabled($includeRemove);
  224. $m .= "<tr><td valign='top' style='border-bottom: 1px solid #555;'>";
  225. $m .= $this->_collections[$key]["_remove"]->getMarkup(
  226. $fieldName."_".$key."__remove");
  227. // Display the list
  228. $m .= "\n<br/>".$this->_collections[$key]["_moveToPosition"]->getMarkup($fieldName."_".$key."__moveToPosition");
  229. $m .= $this->_collections[$key]["_moveToPositionChoice"]->getMarkup($fieldName."_".$key."__moveToPositionChoice");
  230. $m .= "</td><td style='border-bottom: 1px solid #555;'>";
  231. $m .= Wizard::parseText($this->_text, $this->_collections[$key],
  232. $fieldName."_".$key."_");
  233. $m .= "</td></tr>\n";
  234. }
  235. $this->_addButton->setEnabled($includeAdd);
  236. $m .= "<tr><td colspan='2'>".$this->_addButton->getMarkup($fieldName."_add")."</td></tr>\n";
  237. $m .= "</table>\n";
  238. return $m;
  239. }
  240. }
  241. ?>

Documentation generated on Wed, 19 Sep 2007 10:28:12 -0400 by phpDocumentor 1.3.0RC3