Newer
Older
<?php
/**
* This file contains \QUI\Bricks\Manager
*/
namespace QUI\Bricks;
use QUI;
use QUI\Projects\Project;
use QUI\Projects\Site;
use QUI\Utils\Text\XML;
/**
* Brick Manager
*
* @package quiqqer/bricks
*/
class Manager
{
/**
* Bricks table name
*/
const TABLE = 'bricks';
/**
* Bricks uid table name
*/
const TABLE_UID = 'bricksUID';
/**
* Brick Cache table name
*/
const TABLE_CACHE = 'bricksCache';
/**
* Brick temp collector
*
* @var array
*/
/**
* Brick UID temp collector
*
* @var array
*/
protected $brickUIDs = array();
/**
* Initialized brick manager
*
* @var null
*/
/**
* Return the global QUI\Bricks\Manager
*
* @return Manager
*/
}
/**
* Constructor
* Please use \QUI\Bricks\Manager::init()
*
* @param boolean $init - please use \QUI\Bricks\Manager::init()
*/
public function __construct($init = false)
{
if ($init === false) {
QUI\System\Log::addWarning('Please use \QUI\Bricks\Manager::init()');
}
}
/**
* Creates a new brick for the project
*
* @param Project $Project
* @param Brick $Brick
*
* @return integer - Brick-ID
*/
public function createBrickForProject(Project $Project, Brick $Brick)
{
QUI\Permissions\Permission::checkPermission('quiqqer.bricks.create');
QUI::getDataBase()->insert(
$this->getTable(),
array(
'project' => $Project->getName(),
'lang' => $Project->getLang(),
'title' => $Brick->getAttribute('title'),
'description' => $Brick->getAttribute('description'),
'type' => $Brick->getAttribute('type')
)
);
$lastId = QUI::getPDO()->lastInsertId();
return $lastId;
}
/**
* CLears the bricks cache
*/
public function clearCache()
{
QUI\Cache\Manager::clear('quiqqer/bricks');
}
/**
* Delete the brick
*
QUI\Permissions\Permission::checkPermission('quiqqer.bricks.delete');
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// check if brick exist
$this->getBrickById($brickId);
QUI::getDataBase()->delete($this->getTable(), array(
'id' => $brickId
));
}
/**
* Return the areas which are available in the project
*
* @param Project $Project
* @param string|boolean $layoutType - optional, returns only the areas
* for the specific layout type
* (default = false)
* @return array
*/
public function getAreasByProject(Project $Project, $layoutType = false)
{
$templates = array();
$bricks = array();
$projectName = $Project->getName();
if ($Project->getAttribute('template')) {
$templates[] = $Project->getAttribute('template');
}
// get all vhosts, and the used templates of the project
$vhosts = QUI::getRewrite()->getVHosts();
foreach ($vhosts as $vhost) {
if (!isset($vhost['template'])) {
continue;
}
if ($vhost['project'] != $projectName) {
continue;
}
$templates[] = $vhost['template'];
}
$templates = array_unique($templates);
// get bricks
foreach ($templates as $template) {
$brickXML = realpath(OPT_DIR . $template . '/bricks.xml');
if (!$brickXML) {
continue;
}
$bricks = array_merge(
$bricks,
Utils::getTemplateAreasFromXML($brickXML, $layoutType)
);
}
// unque values
$cleaned = array();
foreach ($bricks as $val) {
if (!isset($cleaned[$val['name']])) {
$cleaned[$val['name']] = $val;
}
}
$bricks = array_values($cleaned);
// use @ because: https://bugs.php.net/bug.php?id=50688
@usort($bricks, function ($a, $b) {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
$transA = QUI::getLocale()->get(
$a['title']['group'],
$a['title']['var']
);
$transB = QUI::getLocale()->get(
$b['title']['group'],
$b['title']['var']
);
return $transA > $transB ? 1 : -1;
});
QUI::getEvents()->fireEvent(
'onBricksGetAreaByProject',
array($this, $Project, &$bricks)
);
return $bricks;
}
/**
* Returns the available bricks
*
* @return array
*/
public function getAvailableBricks()
{
$cache = 'quiqqer/bricks/availableBricks';
try {
return QUI\Cache\Manager::get($cache);
} catch (QUI\Exception $Exception) {
}
$xmlFiles = $this->getBricksXMLFiles();
$result = array();
$result[] = array(
'title' => array('quiqqer/bricks', 'brick.content.title'),
'description' => array(
'quiqqer/bricks',
'brick.content.description'
),
'control' => 'content'
);
foreach ($xmlFiles as $bricksXML) {
$result = array_merge($result, Utils::getBricksFromXML($bricksXML));
}
QUI\Cache\Manager::set($cache, $result);
return $result;
}
/**
* Get a Brick by its Brick-ID
*
*
* @return Brick
* @throws QUI\Exception
*/
public function getBrickById($id)
{
'from' => $this->getTable(),
'where' => array(
'id' => (int)$id
),
'limit' => 1
));
if (!isset($data[0])) {
throw new QUI\Exception('Brick not found');
}
$Brick = new Brick($data[0]);
$Brick->setAttribute('id', $id);
$this->bricks[$id] = $Brick;
/**
* Get a Brick by its unique ID
*
* @param string $uid - unique id
*
* @return Brick
* @throws QUI\Exception
*/
public function getBrickByUID($uid)
{
if (isset($this->brickUIDs[$uid])) {
return $this->brickUIDs[$uid];
}
$data = QUI::getDataBase()->fetch(array(
'from' => $this->getTable(),
'where' => array(
'id' => (int)$uid
),
'limit' => 1
));
if (!isset($data[0])) {
throw new QUI\Exception('Brick not found');
}
$Brick = new Brick($data[0]);
$Brick->setAttribute('id', $uid);
$this->brickUIDs[$uid] = $Brick;
return $this->brickUIDs[$uid];
/**
* Return the available brick settings by the brick type
*
* @param $brickType
*
* @return array
*/
public function getAvailableBrickSettingsByBrickType($brickType)
{
$cache = 'quiqqer/bricks/brickType/' . md5($brickType);
try {
return QUI\Cache\Manager::get($cache);
} catch (QUI\Exception $Exception) {
}
$settings = array();
'name' => 'width',
'text' => array('quiqqer/bricks', 'site.area.window.settings.setting.width'),
'type' => '',
'class' => '',
'options' => false
'name' => 'height',
'text' => array('quiqqer/bricks', 'site.area.window.settings.setting.height'),
'type' => '',
'class' => '',
'options' => false
'name' => 'classes',
'text' => array('quiqqer/bricks', 'site.area.window.settings.setting.classes'),
'type' => '',
'class' => '',
'options' => false
$xmlFiles = $this->getBricksXMLFiles();
foreach ($xmlFiles as $brickXML) {
$Dom = XML::getDomFromXml($brickXML);
$Path = new \DOMXPath($Dom);
$Settings = $Path->query(
"//quiqqer/bricks/brick[@control='{$brickType}']/settings/setting"
);
$Globals = $Path->query(
"//quiqqer/bricks/brick[@control='*']/settings/setting"
);
foreach ($Globals as $Setting) {
$settings[] = $this->parseSettingToBrickArray($Setting);
$settings[] = $this->parseSettingToBrickArray($Setting);
}
}
QUI\Cache\Manager::set($cache, $settings);
/**
* Parse a xml setting element to a brick array
*
* @param \DOMElement $Setting
* @return array
*/
protected function parseSettingToBrickArray(\DOMElement $Setting)
{
/* @var $Option \DOMElement */
$options = false;
if ($Setting->getAttribute('type') == 'select') {
$optionElements = $Setting->getElementsByTagName('option');
foreach ($optionElements as $Option) {
$options[] = array(
'value' => $Option->getAttribute('value'),
'text' => QUI\Utils\DOM::getTextFromNode($Option, false)
'name' => $Setting->getAttribute('name'),
'text' => QUI\Utils\DOM::getTextFromNode($Setting, false),
'type' => $Setting->getAttribute('type'),
'class' => $Setting->getAttribute('class'),
'data-qui' => $Setting->getAttribute('data-qui'),
'options' => $options
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
}
/**
* Return the bricks from the area
*
* @param string $brickArea - Name of the area
* @param Site $Site
*
* @return array
*/
public function getBricksByArea($brickArea, Site $Site)
{
if (empty($brickArea)) {
return array();
}
$brickAreas = $Site->getAttribute('quiqqer.bricks.areas');
$brickAreas = json_decode($brickAreas, true);
if (!isset($brickAreas[$brickArea]) || empty($brickAreas[$brickArea])) {
$bricks = $this->getInheritedBricks($brickArea, $Site);
} else {
$bricks = array();
$brickData = $brickAreas[$brickArea];
foreach ($brickData as $brick) {
if (isset($brick['deactivate'])) {
break;
}
$bricks[] = $brick;
}
}
$result = array();
foreach ($bricks as $brickData) {
$brickId = (int)$brickData['brickId'];
try {
$Brick = $this->getBrickById($brickId);
$Clone = clone $Brick;
if (isset($brickData['customfields'])
&& !empty($brickData['customfields'])
) {
$custom = json_decode($brickData['customfields'], true);
if ($custom) {
$Clone->setSettings($custom);
$result[] = $Clone->check();
} catch (QUI\Exception $Exception) {
QUI\System\Log::addWarning(
$Exception->getMessage() . ' Brick-ID:' . $brickId
);
}
}
return $result;
}
/**
* Return a list with \QUI\Bricks\Brick which are assigned to a project
*
* @param Project $Project
*
* @return array
*/
public function getBricksFromProject(Project $Project)
{
$result = array();
$list = QUI::getDataBase()->fetch(array(
'from' => $this->getTable(),
'where' => array(
'project' => $Project->getName(),
'lang' => $Project->getLang()
)
));
foreach ($list as $entry) {
$result[] = $this->getBrickById($entry['id']);
}
return $result;
}
/**
* @param string|integer $brickId - Brick-ID
* @param array $brickData - Brick data
*/
public function saveBrick($brickId, array $brickData)
{
QUI\Permissions\Permission::checkPermission('quiqqer.bricks.edit');
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
$Brick = $this->getBrickById($brickId);
$areas = array();
$areaString = '';
if (isset($brickData['id'])) {
unset($brickData['id']);
}
// check areas
$Project = QUI::getProjectManager()->getProject(
$Brick->getAttribute('project')
);
$availableAreas = array_map(function ($data) {
if (isset($data['name'])) {
return $data['name'];
}
return '';
}, $this->getAreasByProject($Project));
if (isset($brickData['attributes'])
&& isset($brickData['attributes']['areas'])
) {
$brickData['areas'] = $brickData['attributes']['areas'];
}
if (isset($brickData['areas'])) {
$parts = explode(',', $brickData['areas']);
foreach ($parts as $area) {
if (in_array($area, $availableAreas)) {
$areas[] = $area;
}
}
}
if (!empty($areas)) {
$areaString = ',' . implode(',', $areas) . ',';
}
$Brick->setAttributes($brickData);
// fields
if (isset($brickData['attributes'])) {
foreach ($brickData['attributes'] as $key => $value) {
if ($key == 'areas') {
continue;
}
$Brick->setAttribute($key, $value);
}
}
// brick settings
if (isset($brickData['settings'])) {
$Brick->setSettings($brickData['settings']);
}
// custom fields
$customfields = array();
if (isset($brickData['customfields'])) {
$availableSettings = $Brick->getSettings();
$availableSettings['width'] = true;
$availableSettings['height'] = true;
foreach ($brickData['customfields'] as $customfield) {
$customfield = str_replace('flexible-', '', $customfield);
if ($customfield == 'classes') {
$customfields[] = $customfield;
continue;
}
if (isset($availableSettings[$customfield])) {
$customfields[] = $customfield;
}
}
}
// update
QUI::getDataBase()->update($this->getTable(), array(
'title' => $Brick->getAttribute('title'),
'frontendTitle' => $Brick->getAttribute('frontendTitle'),
'description' => $Brick->getAttribute('description'),
'content' => $Brick->getAttribute('content'),
'type' => $Brick->getAttribute('type'),
'settings' => json_encode($Brick->getSettings()),
'customfields' => json_encode($customfields),
'areas' => $areaString,
'height' => $Brick->getAttribute('height'),
'width' => $Brick->getAttribute('width'),
'classes' => json_encode($Brick->getCSSClasses())
), array(
'id' => (int)$brickId
));
}
/**
* Returns the bricks table name
*
* @return String
*/
protected function getTable()
{
return QUI::getDBTableName(self::TABLE);
}
/**
* @return string
*/
protected function getUIDTable()
{
return QUI::getDBTableName(self::TABLE);
}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/**
* List of available bricks.xml files
*
* @return array
*/
protected function getBricksXMLFiles()
{
$cache = 'quiqqer/bricks/availableBrickFiles';
try {
return QUI\Cache\Manager::get($cache);
} catch (QUI\Exception $Exception) {
}
$PKM = QUI::getPackageManager();
$Projects = QUI::getProjectManager();
$packages = $PKM->getInstalled();
$result = array();
// package bricks
foreach ($packages as $package) {
$bricksXML = OPT_DIR . $package['name'] . '/bricks.xml';
if (file_exists($bricksXML)) {
$result[] = $bricksXML;
}
}
// project bricks
$projects = $Projects->getProjects();
foreach ($projects as $project) {
$bricksXML = USR_DIR . $project . '/bricks.xml';
if (file_exists($bricksXML)) {
$result[] = $bricksXML;
}
}
QUI\Cache\Manager::set($cache, $result);
return $result;
}
/**
* Return the bricks from an area which are inherited from its parents
*
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
* @param Site $Site - Site object
*
* @return array
*/
protected function getInheritedBricks($brickArea, Site $Site)
{
// inheritance ( vererbung )
$Project = $Site->getProject();
$areas = $this->getAreasByProject($Project);
foreach ($areas as $area) {
if ($area['name'] != $brickArea) {
continue;
}
if (!$area['inheritance']) {
return array();
}
break;
}
if (!isset($area) || !isset($area['name'])) {
return array();
}
if ($area['name'] != $brickArea) {
return array();
}
if (!Utils::hasInheritance($Project, $brickArea)) {
return array();
}
$result = array();
$parentIds = $Site->getParentIdTree();
$parentIds = array_reverse($parentIds);
$projectCacheTable = QUI::getDBProjectTableName(
self::TABLE_CACHE,
$Project
);
foreach ($parentIds as $parentId) {
$bricks = QUI::getDataBase()->fetch(array(
'from' => $projectCacheTable,
'id' => $parentId,
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
'area' => $brickArea
)
));
if (empty($bricks) || !is_array($bricks)) {
continue;
}
try {
$Parent = $Project->get($parentId);
} catch (QUI\Exception $Exception) {
continue;
}
$parentAreas = $Parent->getAttribute('quiqqer.bricks.areas');
$parentAreas = json_decode($parentAreas, true);
if (!isset($parentAreas[$brickArea])) {
continue;
}
$brickIds = array();
$area = $parentAreas[$brickArea];
foreach ($bricks as $brick) {
$brickIds[$brick['brick']] = true;
}
foreach ($area as $brick) {
if (isset($brick['brickId']) && isset($brickIds[$brick['brickId']])) {