13
13
* Copyright (C) 2012 by Ryan Cramer
14
14
* Licensed under GNU/GPL v2, see LICENSE.TXT
15
15
*
16
- * http://www.processwire.com
17
- * http://www.ryancramer.com
16
+ * http://processwire.com
18
17
*
19
18
*/
20
19
@@ -24,7 +23,7 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule {
24
23
return array(
25
24
'title' => __('Repeater', __FILE__), // Module Title
26
25
'summary' => __('Maintains a collection of fields that are repeated for any number of times.', __FILE__), // Module Summary
27
- 'version' => 100 ,
26
+ 'version' => 101 ,
28
27
'autoload' => true,
29
28
'installs' => 'InputfieldRepeater'
30
29
);
@@ -61,6 +60,10 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule {
61
60
*
62
61
*/
63
62
public function __construct() {
63
+
64
+ require_once(dirname(__FILE__) . '/RepeaterPage.php');
65
+ require_once(dirname(__FILE__) . '/RepeaterPageArray.php');
66
+
64
67
$this->set('repeatersRootPageID', 0);
65
68
}
66
69
@@ -80,6 +83,12 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule {
80
83
public function ready() {
81
84
if(wire('page')->process == 'ProcessPageEdit') $this->addHookBefore('ProcessPageEdit::ajaxSave', $this, 'hookProcessPageEditAjaxSave');
82
85
$this->addHookBefore('PageFinder::getQuery', $this, 'hookPageFinderGetQuery');
86
+
87
+ // make sure that all templates used by repeater pages enforce a Page type of RepeaterPage
88
+ foreach(wire('templates') as $template) {
89
+ if(strpos($template->name, self::templateNamePrefix) === false) continue;
90
+ $template->setQuietly('pageClass', 'RepeaterPage');
91
+ }
83
92
}
84
93
85
94
/**
@@ -410,12 +419,14 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule {
410
419
*/
411
420
public function getBlankRepeaterPage(Page $page, Field $field) {
412
421
$parent = $this->getRepeaterPageParent($page, $field);
413
- $readyPage = new Page ();
422
+ $readyPage = new RepeaterPage ();
414
423
$readyPage->template = $this->getRepeaterTemplate($field);
415
424
if($parent->id) $readyPage->parent = $parent;
416
425
$readyPage->addStatus(Page::statusHidden);
417
426
$readyPage->addStatus(Page::statusUnpublished);
418
427
$readyPage->name = $this->getUniqueRepeaterPageName();
428
+ $readyPage->setForPage($page);
429
+ $readyPage->setForField($field);
419
430
return $readyPage;
420
431
}
421
432
@@ -1266,100 +1277,3 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule {
1266
1277
1267
1278
}
1268
1279
1269
- /**
1270
- * Special PageArray for use by repeaters that includes a getNewItem() method
1271
- *
1272
- */
1273
-
1274
- class RepeaterPageArray extends PageArray {
1275
-
1276
- /**
1277
- * The page that contains the repeater field (not the parent in the repeaters structure)
1278
- *
1279
- */
1280
- protected $parent = null;
1281
-
1282
- /**
1283
- * The repeater field (from $this->fields API var)
1284
- *
1285
- */
1286
- protected $field = null;
1287
-
1288
- public function __construct(Page $parent, Field $field) {
1289
- $this->setParent($parent);
1290
- $this->setField($field);
1291
- }
1292
-
1293
- public function setParent(Page $parent) { $this->parent = $parent; }
1294
- public function getParent() { return $this->parent; }
1295
- public function setField(Field $field) { $this->field = $field; }
1296
- public function getField() { return $this->field; }
1297
-
1298
- /**
1299
- * Alias of getNewItem() kept for backwards compatibility
1300
- *
1301
- */
1302
- public function getNew() { return $this->getNewItem(); }
1303
-
1304
- /**
1305
- * Return a new repeater item ready for use
1306
- *
1307
- * If there are ready items, it will return the first ready item
1308
- * Otherwise it'll create a new item
1309
- *
1310
- * This method is different from FieldtypeRepeater::getBlankRepeaterPage:
1311
- * 1. It returns an already existing readyPage, if it exists (otherwise it creates a new page)
1312
- * 2. The returned page is in a non-hidden published state, so will appear as soon as it is saved
1313
- *
1314
- * Note that this method has no relation/similarity to the makeNew() method.
1315
- *
1316
- * @return Page
1317
- *
1318
- */
1319
- public function getNewItem() {
1320
-
1321
- $page = null;
1322
- $of = $this->parent->of(false);
1323
-
1324
- foreach($this as $item) {
1325
- if($item->is(Page::statusUnpublished) && $item->is(Page::statusHidden)) {
1326
- $page = $item;
1327
- break;
1328
- }
1329
- }
1330
-
1331
- if(is_null($page)) {
1332
- $page = $this->field->type->getBlankRepeaterPage($this->parent, $this->field);
1333
- $this->add($page);
1334
- } else {
1335
- $this->trackChange('add');
1336
- }
1337
-
1338
- $page->of(false);
1339
- $page->removeStatus(Page::statusUnpublished);
1340
- $page->removeStatus(Page::statusHidden);
1341
- $page->sort = $this->count();
1342
-
1343
- if($of) $this->parent->of(true);
1344
-
1345
- return $page;
1346
- }
1347
-
1348
- /**
1349
- * Creates a new blank instance of a RepeaterPageArray. For internal use.
1350
- *
1351
- * Note that this method has no relation/similarity to the getNewItem()/getNew() methods.
1352
- *
1353
- * @param array $items Array of items to populate (optional)
1354
- * @return WireArray
1355
- *
1356
- */
1357
- public function makeNew() {
1358
- $class = get_class($this);
1359
- $newArray = new $class($this->parent, $this->field);
1360
- return $newArray;
1361
- }
1362
-
1363
-
1364
- }
1365
-
0 commit comments