PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wire/modules/Fieldtype/FieldtypeRepeater/RepeaterPageArray.php

http://github.com/ryancramerdesign/ProcessWire
PHP | 108 lines | 41 code | 18 blank | 49 comment | 5 complexity | 8f0d11cb038ba14317241968e774a2f4 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ProcessWire Repeater Page Array
  4. *
  5. * Special PageArray for use by repeaters that includes a getNewItem() method
  6. *
  7. * ProcessWire 2.x
  8. * Copyright (C) 2015 by Ryan Cramer
  9. * This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
  10. *
  11. * https://processwire.com
  12. *
  13. */
  14. class RepeaterPageArray extends PageArray {
  15. /**
  16. * The page that contains the repeater field (not the parent in the repeaters structure)
  17. *
  18. */
  19. protected $parent = null;
  20. /**
  21. * The repeater field (from $this->fields API var)
  22. *
  23. */
  24. protected $field = null;
  25. public function __construct(Page $parent, Field $field) {
  26. $this->setParent($parent);
  27. $this->setField($field);
  28. }
  29. public function setParent(Page $parent) { $this->parent = $parent; }
  30. public function getParent() { return $this->parent; }
  31. public function setField(Field $field) { $this->field = $field; }
  32. public function getField() { return $this->field; }
  33. /**
  34. * Alias of getNewItem() kept for backwards compatibility
  35. *
  36. */
  37. public function getNew() { return $this->getNewItem(); }
  38. /**
  39. * Return a new repeater item ready for use
  40. *
  41. * If there are ready items, it will return the first ready item
  42. * Otherwise it'll create a new item
  43. *
  44. * This method is different from FieldtypeRepeater::getBlankRepeaterPage:
  45. * 1. It returns an already existing readyPage, if it exists (otherwise it creates a new page)
  46. * 2. The returned page is in a non-hidden published state, so will appear as soon as it is saved
  47. *
  48. * Note that this method has no relation/similarity to the makeNew() method.
  49. *
  50. * @return Page
  51. *
  52. */
  53. public function getNewItem() {
  54. $page = null;
  55. $of = $this->parent->of(false);
  56. // first try to get a ready item, if available
  57. foreach($this as $item) {
  58. if($item->hasStatus(Page::statusUnpublished) && $item->hasStatus(Page::statusHidden)) {
  59. $page = $item;
  60. break;
  61. }
  62. }
  63. if(is_null($page)) {
  64. // no ready item available, get a new one
  65. $page = $this->field->type->getBlankRepeaterPage($this->parent, $this->field);
  66. $this->add($page);
  67. } else {
  68. $this->trackChange('add');
  69. }
  70. $page->of(false);
  71. $page->removeStatus(Page::statusUnpublished);
  72. $page->removeStatus(Page::statusHidden);
  73. $page->sort = $this->count();
  74. if($of) $this->parent->of(true);
  75. return $page;
  76. }
  77. /**
  78. * Creates a new blank instance of a RepeaterPageArray. For internal use.
  79. *
  80. * Note that this method has no relation/similarity to the getNewItem()/getNew() methods.
  81. *
  82. * @return WireArray
  83. *
  84. */
  85. public function makeNew() {
  86. $class = get_class($this);
  87. $newArray = new $class($this->parent, $this->field);
  88. return $newArray;
  89. }
  90. }