Skip to content
Code-Schnipsel Gruppen Projekte
Manager.php 17,6 KiB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
<?php

/**
 * This file contains \QUI\Bricks\Manager
 */

namespace QUI\Bricks;

use QUI;
use QUI\Projects\Project;
use QUI\Projects\Site;

/**
 * Brick Manager
 *
 * @package quiqqer/bricks
 */
class Manager
{
    /**
     * Bricks table name
     */
    const TABLE = 'bricks';

    /**
     * Brick Cache table name
     */
    const TABLE_CACHE = 'bricksCache';

    /**
     * Brick temp collector
     *
     * @var array
     */
Henning Leutz's avatar
Henning Leutz committed
    protected $bricks = array();
Henning Leutz's avatar
Henning Leutz committed

    /**
     * Initialized brick manager
     *
     * @var null
     */
Henning Leutz's avatar
Henning Leutz committed
    public static $BrickManager = null;
Henning Leutz's avatar
Henning Leutz committed

    /**
     * Return the global QUI\Bricks\Manager
     *
     * @return Manager
     */
Henning Leutz's avatar
Henning Leutz committed
    public static function init()
Henning Leutz's avatar
Henning Leutz committed
    {
Henning Leutz's avatar
Henning Leutz committed
        if (is_null(self::$BrickManager)) {
            self::$BrickManager = new QUI\Bricks\Manager(true);
Henning Leutz's avatar
Henning Leutz committed
        return self::$BrickManager;
Henning Leutz's avatar
Henning Leutz committed
    }

    /**
     * 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\Rights\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
     *
     * @param Integer $brickId - Brick-ID
     */
    public function deleteBrick($brickId)
    {
        QUI\Rights\Permission::checkPermission('quiqqer.bricks.delete');

        // 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);

        usort($bricks, function ($a, $b) {

            $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
     *
     * @param Integer $id
     *
     * @return Brick
     * @throws QUI\Exception
     */
    public function getBrickById($id)
    {
Henning Leutz's avatar
Henning Leutz committed
        if (isset($this->bricks[$id])) {
            return $this->bricks[$id];
Henning Leutz's avatar
Henning Leutz committed
        }

        $data = QUI::getDataBase()->fetch(array(
            'from'  => $this->getTable(),
            'where' => array(
                'id' => (int)$id
            ),
            'limit' => 1
        ));

        if (!isset($data[0])) {
            throw new QUI\Exception('Brick not found');
        }

Henning Leutz's avatar
Henning Leutz committed
        $this->bricks[$id] = new Brick($data[0]);
        $this->bricks[$id]->setAttribute('id', $id);
Henning Leutz's avatar
Henning Leutz committed

Henning Leutz's avatar
Henning Leutz committed
        return $this->bricks[$id];
Henning Leutz's avatar
Henning Leutz committed
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 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 616 617 618 619 620 621 622 623 624 625 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 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 698 699 700 701 702 703 704 705 706 707
    }

    /**
     * Return the available brick settings by the brick type
     *
     * @param $brickType
     *
     * @return array
     */
    public function getAvailableBrickSettingsByBrickType($brickType)
    {
        if ($brickType == 'content') {
            return array();
        }

        $cache = 'quiqqer/bricks/brickType/' . md5($brickType);

        try {
            return QUI\Cache\Manager::get($cache);

        } catch (QUI\Exception $Exception) {
        }


        $settings = array();
        $xmlFiles = $this->getBricksXMLFiles();

        foreach ($xmlFiles as $brickXML) {
            $Dom  = QUI\Utils\XML::getDomFromXml($brickXML);
            $Path = new \DOMXPath($Dom);

            $Settings = $Path->query(
                "//quiqqer/bricks/brick[@control='{$brickType}']/settings/setting"
            );

            if (!$Settings->length) {
                continue;
            }

            foreach ($Settings as $Setting) {
                /* @var $Setting \DOMElement */
                /* @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)
                        );
                    }
                }

                $settings[] = array(
                    '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
                );
            }

            break;
        }

        QUI\Cache\Manager::set($cache, $settings);

        return $settings;
    }

    /**
     * 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);

                if (isset($brickData['customfields'])
                    && !empty($brickData['customfields'])
                ) {
                    $custom = json_decode($brickData['customfields'], true);

                    if ($custom) {
                        $Brick->setSettings($custom);
                    }
                }

                $result[] = $Brick->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\Rights\Permission::checkPermission('quiqqer.bricks.edit');

        $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'),
            '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'      => $Brick->getAttribute('classes')
        ), array(
            'id' => (int)$brickId
        ));
    }

    /**
     * Returns the bricks table name
     *
     * @return String
     */
    protected function getTable()
    {
        return QUI::getDBTableName(self::TABLE);
    }

    /**
     * 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
     *
     * @param String $brickArea - Name of the area
     * @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,
                'where' => array(
                    'id'   => $parentId,
                    '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($brickIds[$brick['brickId']])) {
                    $result[] = $brick;
                }
            }

            if (empty($result)) {
                continue;
            }

            break;
        }

        return $result;
    }
}