2022-03-08 15:55:41 +01:00
|
|
|
<?php namespace ProcessWire;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ajax handler for FieldtypePageTable/InputfieldPageTable
|
|
|
|
*
|
|
|
|
* Concept by Antti Peisa
|
|
|
|
* Code by Ryan Cramer
|
|
|
|
* Sponsored by Avoine
|
|
|
|
*
|
2024-04-04 14:37:20 +02:00
|
|
|
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
2022-03-08 15:55:41 +01:00
|
|
|
* https://processwire.com
|
|
|
|
*
|
|
|
|
* @method void checkAjax()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class InputfieldPageTableAjax extends Wire {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notes to display in the InputfieldPageTable's notes section
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $notes = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->checkAjax();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if current request is a valid ajax request and call renderAjax() if it is.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function ___checkAjax() {
|
|
|
|
|
2024-04-04 14:37:20 +02:00
|
|
|
$pages = $this->wire()->pages;
|
|
|
|
$input = $this->wire()->input;
|
|
|
|
|
2022-03-08 15:55:41 +01:00
|
|
|
$fieldName = $input->get('InputfieldPageTableField');
|
|
|
|
if(!$fieldName) return;
|
|
|
|
|
2024-04-04 14:37:20 +02:00
|
|
|
$processPage = $this->wire()->page;
|
2022-03-08 15:55:41 +01:00
|
|
|
if(!in_array('WirePageEditor', wireClassImplements((string) $processPage->process))) return; // not ProcessPageEdit or compatible
|
|
|
|
|
2024-04-04 14:37:20 +02:00
|
|
|
$field = $this->wire()->fields->get($this->wire()->sanitizer->fieldName($fieldName));
|
2022-03-08 15:55:41 +01:00
|
|
|
if(!$field || !$field->type instanceof FieldtypePageTable) return; // die('field does not exist or is not FieldtypePageTable');
|
|
|
|
|
|
|
|
$pageID = (int) $input->get('id');
|
|
|
|
if(!$pageID) return; // die('page ID not specified');
|
|
|
|
|
2024-04-04 14:37:20 +02:00
|
|
|
$page = $pages->get($pageID);
|
2022-03-08 15:55:41 +01:00
|
|
|
if(!$page->id) return;
|
|
|
|
if(!$page->editable($field->name)) return;
|
|
|
|
|
|
|
|
$page->of(false);
|
|
|
|
$page->get($field->name); // preload, fixes issue #518 with formatted version getting loaded when it shouldn't
|
|
|
|
|
|
|
|
// check for new item that should be added
|
|
|
|
$itemID = (int) $input->get('InputfieldPageTableAdd');
|
2024-04-04 14:37:20 +02:00
|
|
|
if($itemID) $this->addItem($page, $field, $pages->get($itemID));
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
$sort = $input->get('InputfieldPageTableSort');
|
2022-11-05 18:32:48 +01:00
|
|
|
if(strlen("$sort")) $this->sortItems($page, $field, $sort);
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
$this->renderAjax($page, $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the ajax request output directly and halt execution
|
|
|
|
*
|
|
|
|
* @param Page $page
|
|
|
|
* @param Field $field
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function renderAjax(Page $page, Field $field) {
|
|
|
|
$inputfield = $field->getInputfield($page);
|
|
|
|
if(!$inputfield) return;
|
|
|
|
echo $inputfield->render();
|
|
|
|
if($this->notes) {
|
2024-04-04 14:37:20 +02:00
|
|
|
echo "<p class='notes'>" . $this->wire()->sanitizer->entities($this->notes) . "</p>";
|
2022-03-08 15:55:41 +01:00
|
|
|
$this->notes = '';
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the InputfieldPageTableAdd ajax action
|
|
|
|
*
|
|
|
|
* @param Page $page
|
|
|
|
* @param Field $field
|
|
|
|
* @param Page $item
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function addItem(Page $page, Field $field, Page $item) {
|
|
|
|
// add an item and save the field
|
2024-04-04 14:37:20 +02:00
|
|
|
if(!$item->id || $item->createdUser->id !== $this->wire()->user->id) return false;
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
$value = $page->getUnformatted($field->name);
|
|
|
|
|
|
|
|
if($value instanceof PageArray && !$value->has($item)) {
|
|
|
|
$of = $page->of();
|
|
|
|
$page->of(false);
|
|
|
|
$value->add($item);
|
|
|
|
$page->set($field->name, $value);
|
|
|
|
$page->save($field->name);
|
|
|
|
$this->notes = $this->_('Added item') . ' - ' . $item->name;
|
|
|
|
$page->of($of);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update items to make sure they are in same order specified in GET var InputfieldPageTableSort
|
|
|
|
*
|
|
|
|
* @param Page $page
|
|
|
|
* @param Field $field
|
|
|
|
* @param string $sort CSV string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function sortItems(Page $page, Field $field, $sort) {
|
|
|
|
|
|
|
|
// if this field has it's own sort settings, then we have nothing to do here.
|
|
|
|
if($field->get('sortfields') && $field->get('sortfields') != 'sort') return;
|
|
|
|
|
|
|
|
$value = $page->getUnformatted($field->name);
|
|
|
|
if(!$value instanceof PageArray || !$value->count()) return;
|
|
|
|
$sortedIDs = explode(',', $sort);
|
|
|
|
|
|
|
|
$test = implode('|', $sortedIDs);
|
|
|
|
if($test == ((string) $value)) return; // already in right order?
|
|
|
|
|
|
|
|
foreach($value as $item) {
|
|
|
|
$sort = array_search($item->id, $sortedIDs);
|
|
|
|
if($sort === false) $sort = count($value);
|
|
|
|
$item->set('_pagetable_sort', $sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
$value->sort('_pagetable_sort');
|
|
|
|
}
|
|
|
|
}
|