/
PageTypeField.php
83 lines (72 loc) · 2.53 KB
/
PageTypeField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Concrete\Core\Page\Search\Field\Field;
use Concrete\Core\Backup\ContentExporter;
use Concrete\Core\File\FileList;
use Concrete\Core\File\Type\Type;
use Concrete\Core\Search\Field\AbstractField;
use Concrete\Core\Search\Field\FieldInterface;
use Concrete\Core\Search\ItemList\ItemList;
class PageTypeField extends AbstractField
{
protected $requestVariables = [
'ptID'
];
public function getKey()
{
return 'page_type';
}
public function getDisplayName()
{
return t('Page Type');
}
/**
* @param FileList $list
* @param $request
*/
public function filterList(ItemList $list)
{
$list->filterByPageTypeID($this->getData('ptID'));
}
public function renderSearchField()
{
$html = '';
$types = \Core::make('site/type')->getList();
if (count($types) == 1) {
$form = \Core::make('helper/form');
$html = $form->select('ptID', array_reduce(
\PageType::getList(), function ($types, $type) {
$types[$type->getPageTypeID()] = $type->getPageTypeDisplayName();
return $types;
}
), $this->getData('ptID'));
} else {
$html = '<select name="ptID" class="form-select">';
foreach($types as $type) {
$html .= '<optgroup label="' . $type->getSiteTypeName() . '">';
$types = \PageType::getList(false, $type);
foreach($types as $pageType) {
$selected = $pageType->getPageTypeID() == $this->getData('ptID') ? ' selected ' : '';
$html .= '<option value="' . $pageType->getPageTypeID() . '" ' . $selected . '>' . $pageType->getPageTypeDisplayName() . '</option>';
}
$html .= '</optgroup>';
}
$html .= '</select>';
}
return $html;
}
public function export(\SimpleXMLElement $element)
{
$fieldNode = $element->addChild('field');
$fieldNode->addAttribute('key', $this->getKey());
$fieldNode->addChild('pagetype', ContentExporter::replacePageTypeWithPlaceHolder($this->getData('ptID')));
}
public function loadDataFromImport(\SimpleXMLElement $element)
{
if (!$this->isLoaded) {
$inspector = app('import/value_inspector');
$result = $inspector->inspect((string) $element->pagetype);
$this->data['ptID'] = $result->getReplacedValue();
$this->isLoaded = true;
}
}
}