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()');
}
}
/**
* Returns the bricks table name
*
* @return String
*/
public static function getTable()
{
return QUI::getDBTableName(self::TABLE);
}
/**
* @return string
*/
public static function getUIDTable()
{
return QUI::getDBTableName(self::TABLE_UID);
}
/**
* 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;
}

Henning Leutz
committed
/**
* Create and update a unique site brick
*
* @param Site $Site
* @param array $brickData
* @return string - Unique ID
*/
public function createUniqueSiteBrick(Site $Site, $brickData = array())
{
if (isset($brickData['uid']) || empty($brickData['uid'])) {

Henning Leutz
committed
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
$uid = $brickData['uid'];
if ($this->existsUniqueBrickId($uid) === false) {
$uid = $this->createUniqueBrickId((int)$brickData['brickId'], $Site);
}
} else {
$uid = $this->createUniqueBrickId((int)$brickData['brickId'], $Site);
}
$customFields = array();
if (isset($brickData['customfields'])) {
$customFields = $brickData['customfields'];
}
if (is_array($customFields)) {
$customFields = json_encode($customFields);
}
QUI::getDataBase()->update($this->getUIDTable(), array(
'customfields' => $customFields
), array(
'uid' => $uid
));
return $uid;
}
/**
* Create a new unique Brick ID
*
* @param integer $brickId - Brick ID
* @param Site $Site - Current Site
* @return bool
*/
protected function createUniqueBrickId($brickId, $Site)
{
$Project = $Site->getProject();

Henning Leutz
committed
$Brick = $this->getBrickById($brickId);
QUI::getDataBase()->insert($this->getUIDTable(), array(
'uid' => $uuid,
'brickId' => $brickId,
'project' => $Project->getName(),
'lang' => $Project->getLang(),
'siteId' => $Site->getId(),
'attributes' => json_encode($Brick->getAttributes())
));

Henning Leutz
committed

Henning Leutz
committed
}
/**
* Check if an unique brick ID exists
*
* @param string $uid - Brick Unique ID
* @return bool
*/
public function existsUniqueBrickId($uid)
{
$result = QUI::getDataBase()->fetch(array(
'from' => $this->getUIDTable(),
'where' => array(
'uid' => $uid
),
'limit' => 1
));
return isset($result[0]);
}
/**
* CLears the bricks cache
*/
public function clearCache()
{
QUI\Cache\Manager::clear('quiqqer/bricks');
}
/**
* Delete the brick
*
QUI\Permissions\Permission::checkPermission('quiqqer.bricks.delete');

Henning Leutz
committed
$Brick = $this->getBrickById($brickId);
QUI::getDataBase()->delete($this->getTable(), array(
'id' => $brickId
));

Henning Leutz
committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
if (isset($this->bricks[$brickId])) {
unset($this->bricks[$brickId]);
}
$uniqueBrickIds = QUI::getDataBase()->fetch(array(
'select' => 'siteId, project, lang',
'from' => QUI\Bricks\Manager::getUIDTable(),
'where' => array(
'brickId' => $brickId,
'project' => $Brick->getAttribute('project'),
'lang' => $Brick->getAttribute('lang')
),
'group' => 'siteId, project, lang'
));
// delete bricks in sites
foreach ($uniqueBrickIds as $uniqueBrickId) {
$project = $uniqueBrickId['project'];
$lang = $uniqueBrickId['lang'];
$Project = QUI::getProject($project, $lang);
$Site = $Project->get($uniqueBrickId['siteId']);
$Edit = $Site->getEdit();
$Edit->load();
$Edit->save(QUI::getUsers()->getSystemUser());
}
// delete unique ids
QUI::getDataBase()->delete(QUI\Bricks\Manager::getUIDTable(), array(
'brickId' => $brickId,
'project' => $Brick->getAttribute('project'),
'lang' => $Brick->getAttribute('lang')
));
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
}
/**
* 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)
);
}
// unique 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) {
if (isset($a['priority']) && isset($b['priority'])) {
if ($a['priority'] == $b['priority']) {
return 0;
}
return ($a['priority'] < $b['priority']) ? -1 : 1;
}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
$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));
}
$result = array_filter($result, function ($brick) {
return !empty($brick['title']);
});
// js workaround
$list = array();
foreach ($result as $entry) {
$list[] = $entry;
}
QUI\Cache\Manager::set($cache, $list);
}
/**
* 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(

Henning Leutz
committed
'from' => $this->getUIDTable(),

Henning Leutz
committed
'uid' => $uid
),
'limit' => 1
));
if (!isset($data[0])) {
throw new QUI\Exception('Brick not found');
}

Henning Leutz
committed
$data = $data[0];
$brickId = $data['brickId'];
$custom = $data['customfields'];
$attributes = $data['attributes'];
$attributes = json_decode($attributes, true);
$real = QUI::getDataBase()->fetch(array(
'from' => $this->getTable(),
'where' => array(
'id' => (int)$brickId
),
'limit' => 1
));
$Original = new Brick($real[0]);

Henning Leutz
committed
$Original->setAttribute('id', $brickId);
$Clone = clone $Original;
if (!empty($custom)) {
$custom = json_decode($custom, true);
if ($custom) {
$Clone->setSettings($custom);
}
// workaround
if (isset($custom['brickTitle'])) {
$Clone->setAttribute('frontendTitle', $custom['brickTitle']);
}
}

Henning Leutz
committed
$this->brickUIDs[$uid] = $Clone;

Henning Leutz
committed
return $Clone;
/**
* 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
}
/**
* Return the bricks from the area
*
* @param string $brickArea - Name of the area
* @param QUI\Interfaces\Projects\Site $Site
public function getBricksByArea($brickArea, QUI\Interfaces\Projects\Site $Site)
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
{
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();

Henning Leutz
committed
foreach ($bricks as $key => $brickData) {
$brickId = (int)$brickData['brickId'];
try {

Henning Leutz
committed
if (isset($brickData['uid'])) {
$Brick = $this->getBrickByUID($brickData['uid']);
$result[] = $Brick->check();
continue;
}
// fallback
$Clone = clone $Brick;
if (isset($brickData['customfields'])
&& !empty($brickData['customfields'])
) {
$custom = json_decode($brickData['customfields'], true);
if ($custom) {
$Clone->setSettings($custom);
$result[] = $Clone->check();

Henning Leutz
committed
QUI\System\Log::writeRecursive($brickData);
QUI\System\Log::writeException($Exception);
}
}
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');
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
746
747
748
749
750
751
752
753
754
755
756
757
$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']);
}
$brickAttributes = Utils::getAttributesForBrick($Brick);
foreach ($brickAttributes as $attribute) {
if (isset($brickData['attributes'][$attribute])) {
$Brick->setSetting($attribute, $brickData['attributes'][$attribute]);
}
}
// 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())

Henning Leutz
committed

Henning Leutz
committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
// refresh all bricks with this id
$uniqueBricks = QUI::getDataBase()->fetch(array(
'from' => QUI\Bricks\Manager::getUIDTable(),
'where' => array(
'project' => $Project->getName(),
'lang' => $Project->getLang(),
'brickId' => (int)$brickId
)
));
foreach ($uniqueBricks as $uniqueBrick) {
$customFieldsUniqueBrick = json_decode($uniqueBrick['customfields'], true);
$attributes = $Brick->getAttributes();
if (isset($attributes['attributes'])) {
unset($attributes['attributes']);
}
if (!is_array($customFieldsUniqueBrick)) {
$customFieldsUniqueBrick = array();
}
QUI::getDataBase()->update(QUI\Bricks\Manager::getUIDTable(), array(
'customfields' => json_encode($customFieldsUniqueBrick),
'attributes' => json_encode($attributes)
), array(
'uid' => $uniqueBrick['uid']
));
}
* @param integer|string $brickId
* @param array $params - project, lang, title, description
* @return integer
*
* @throws QUI\Exception
public function copyBrick($brickId, $params = array())
QUI\Permissions\Permission::checkPermission('quiqqer.bricks.create');
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
$result = QUI::getDataBase()->fetch(array(
'from' => $this->getTable(),
'where' => array(
'id' => $brickId
)
));
if (!isset($result[0])) {
throw new QUI\Exception('Brick not found');
}
$allowed = array('project', 'lang', 'title', 'description');
$allowed = array_flip($allowed);
$data = $result[0];
unset($data['id']);
if (!is_array($params)) {
$params = array();
}
foreach ($params as $param => $value) {
if (!isset($allowed[$param])) {
continue;
}
$data[$param] = $value;
}
QUI::getDataBase()->insert($this->getTable(), $data);
$lastId = QUI::getPDO()->lastInsertId();
return $lastId;
/**
* List of available bricks.xml files
*
* @return array
*/
protected function getBricksXMLFiles()
{
}
/**
* Return the bricks from an area which are inherited from its parents
*
* @param QUI\Interfaces\Projects\Site $Site - Site object
protected function getInheritedBricks($brickArea, QUI\Interfaces\Projects\Site $Site)
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
{
// 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,
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'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) {