/
BlockTypeList.php
144 lines (128 loc) · 4.92 KB
/
BlockTypeList.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Concrete\Core\Block\BlockType;
use Concrete\Core\Entity\Block\BlockType\BlockType as BlockTypeEntity;
use Concrete\Core\Support\Facade\Application;
use Core;
use Loader;
use Package;
use Concrete\Core\Legacy\DatabaseItemList;
class BlockTypeList extends DatabaseItemList
{
protected $autoSortColumns = array('btHandle', 'btID', 'btDisplayOrder');
protected $includeInternalBlockTypes = false;
public function __construct()
{
$this->setQuery("select btID from BlockTypes");
$this->sortByMultiple('btDisplayOrder asc', 'btName asc', 'btID asc');
}
public function includeInternalBlockTypes()
{
$this->includeInternalBlockTypes = true;
}
public function get($itemsToGet = 0, $offset = 0)
{
if (!$this->includeInternalBlockTypes) {
$this->filter('btIsInternal', false);
}
$r = parent::get($itemsToGet, intval($offset));
$blocktypes = array();
foreach ($r as $row) {
$bt = BlockType::getByID($row['btID']);
if (is_object($bt)) {
$blocktypes[] = $bt;
}
}
return $blocktypes;
}
public function filterByPackage($pkg)
{
$this->filter('pkgID', $pkg->getPackageID());
}
/**
* @todo comment this one
*
* @param string $xml
*/
public static function exportList($xml)
{
$btl = new static();
$blocktypes = $btl->get();
$nxml = $xml->addChild('blocktypes');
foreach ($blocktypes as $bt) {
$type = $nxml->addChild('blocktype');
$type->addAttribute('handle', $bt->getBlockTypeHandle());
$type->addAttribute('package', $bt->getPackageHandle());
}
}
/**
* Gets a list of block types that are not installed, used to get blocks that can be installed
* This function only surveys the web/blocks directory - it's not looking at the package level.
*
* @return BlockType[]
*/
public static function getAvailableList()
{
$blocktypes = array();
$dir = DIR_FILES_BLOCK_TYPES;
$db = Loader::db();
$btHandles = $db->GetCol("select btHandle from BlockTypes order by btDisplayOrder asc, btName asc, btID asc");
$aDir = array();
if (is_dir($dir)) {
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
if (strpos($file, '.') === false) {
$fdir = $dir . '/' . $file;
if (is_dir($fdir) && !in_array($file, $btHandles) && file_exists($fdir . '/' . FILENAME_BLOCK_CONTROLLER)) {
$bt = BlockType::getByHandle($file);
if (!is_object($bt)) {
$bt = new BlockTypeEntity();
$bt->setBlockTypeHandle($file);
$class = $bt->getBlockTypeClass();
$bta = Application::getFacadeApplication()->build($class);
$bt->setBlockTypeName($bta->getBlockTypeName());
$bt->setBlockTypeDescription($bta->getBlockTypeDescription());
$bt->hasCustomViewTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_VIEW);
$bt->hasCustomEditTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_EDIT);
$bt->hasCustomAddTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_ADD);
$bt->installed = false;
} else {
$bt->installed = true;
}
$blocktypes[] = $bt;
}
}
}
}
return $blocktypes;
}
/**
* gets a list of installed BlockTypes.
*
* @return BlockType[]
*/
public static function getInstalledList()
{
$btl = new static();
return $btl->get();
}
public static function resetBlockTypeDisplayOrder($column = 'btID')
{
$db = Loader::db();
/** @var \Concrete\Core\Cache\Cache $cache */
$cache = Core::make('cache');
$stmt = $db->Prepare("UPDATE BlockTypes SET btDisplayOrder = ? WHERE btID = ?");
$btDisplayOrder = 1;
$blockTypes = $db->GetArray("SELECT btID, btHandle, btIsInternal FROM BlockTypes ORDER BY {$column} ASC");
foreach ($blockTypes as $bt) {
if ($bt['btIsInternal']) {
$db->Execute($stmt, array(0, $bt['btID']));
} else {
$db->Execute($stmt, array($btDisplayOrder, $bt['btID']));
++$btDisplayOrder;
}
$cache->delete('blockTypeByID/' .$bt['btID']);
$cache->delete('blockTypeByHandle/' . $bt['btHandle']);
}
$cache->delete('blockTypeList');
}
}