File "unitecreator_layouts.class.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/inc_php/layouts/unitecreator_layouts.class.php
File size: 18.83 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteCreatorLayoutsWork extends UniteElementsBaseUC
{
    protected $lastDuplicatedID;
    private function a_GET_LAYOUTS()
    {
    }
    private function getWhereFromParams($params)
    {
        $where = array();
        $catID = UniteFunctionsUC::getVal($params, "catid");
        if (!empty($catID)) {
            $where["catid"] = $catID;
        }
        $layoutType = UniteFunctionsUC::getVal($params, "layout_type");
        if (empty($layoutType)) {
            $layoutType = UniteCreatorDB::ISNULL;
        }
        $where["layout_type"] = $layoutType;
        return $where;
    }
    private function getRecords($order = "ordering", $params = array(), $layoutType = null)
    {
        if (empty($params)) {
            $params = array();
        }
        $params["layout_type"] = $layoutType;
        if (empty($order)) {
            $order = "ordering";
        }
        $where = $this->getWhereFromParams($params);
        $response = $this->db->fetch(GlobalsUC::$table_layouts, $where, $order);
        return $response;
    }
    private function getRecordsPaging($pagingOptions)
    {
        $arrWhere = array();
        $search = UniteFunctionsUC::getVal($pagingOptions, "search");
        if (!empty($search)) {
            $search = $this->db->escape($search);
            $arrWhere["title"] = array("LIKE", "%{$search}%");
        }
        $order = UniteFunctionsUC::getVal($pagingOptions, "ordering");
        $filterCat = UniteFunctionsUC::getVal($pagingOptions, "category");
        if (!empty($filterCat) && $filterCat != 'all') {
            $filterCat = (int) $filterCat;
            $arrWhere["catid"] = $filterCat;
        }
        $layoutType = UniteFunctionsUC::getVal($pagingOptions, "layout_type");
        if (empty($layoutType)) {
            $layoutType = UniteCreatorDB::ISNULL;
        }
        $arrWhere["layout_type"] = $layoutType;
        $response = $this->db->fetchPage(GlobalsUC::$table_layouts, $pagingOptions, $arrWhere, $order);
        return $response;
    }
    private function getLayoutFromData($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layout_id");
        UniteFunctionsUC::validateNumeric($layoutID);
        UniteFunctionsUC::validateNotEmpty($layoutID);
        $objLayout = new UniteCreatorLayout();
        $objLayout->initByID($layoutID);
        return $objLayout;
    }
    private function recordsToLayouts($records)
    {
        if (empty($records)) {
            return array();
        }
        $arrLayouts = array();
        foreach ($records as $record) {
            $layoutID = UniteFunctionsUC::getVal($record, "id");
            $objLayout = new UniteCreatorLayout();
            $objLayout->initByID($layoutID);
            $arrLayouts[] = $objLayout;
        }
        return $arrLayouts;
    }
    public function getArrLayouts($order = "ordering", $params = array(), $layoutType = null)
    {
        $response = $this->getRecords($order, $params, $layoutType);
        $arrLayouts = $this->recordsToLayouts($response);
        return $arrLayouts;
    }
    public function getArrLayoutsPaging($pagingOptions)
    {
        $response = $this->getRecordsPaging($pagingOptions);
        $rows = $response["rows"];
        unset($response["rows"]);
        $arrLayouts = $this->recordsToLayouts($rows);
        $output = array();
        $output["paging"] = $response;
        $output["layouts"] = $arrLayouts;
        return $output;
    }
    public function getArrLayoutsShort($addEmpty = false, $params = array(), $layoutType = null)
    {
        if ($layoutType == GlobalsUC::ADDON_TYPE_REGULAR_LAYOUT) {
            $layoutType = null;
        }
        if (!empty($layoutType)) {
            $params["layout_type"] = $layoutType;
        }
        $where = $this->getWhereFromParams($params);
        $arrLayouts = $this->db->fetchFields(GlobalsUC::$table_layouts, "id, title", $where, "ordering");
        if (empty($arrLayouts)) {
            $arrLayouts = array();
        }
        if ($addEmpty == true) {
            $arrItem = array("id" => "empty", "title" => "[Not Selected]");
            $arrAdd = array($arrItem);
            $arrLayouts = array_merge($arrAdd, $arrLayouts);
        }
        return $arrLayouts;
    }
    public function getArrLayoutsKeyValue($addEmpty = false, $layoutType = null)
    {
        if (!empty($layoutType)) {
            $params["layout_type"] = $layoutType;
        }
        $where = $this->getWhereFromParams($params);
        $arrLayouts = $this->db->fetchFields(GlobalsUC::$table_layouts, "id, title", $where, "ordering");
        if (empty($arrLayouts)) {
            $arrLayouts = array();
        }
        if ($addEmpty == true) {
            $arrItem = array("id" => "empty", "title" => "[Not Selected]");
            $arrAdd = array($arrItem);
            $arrLayouts = array_merge($arrAdd, $arrLayouts);
        }
        return $arrLayouts;
    }
    public function getActiveTempaltePart($layoutType)
    {
        $arrLayouts = $this->getArrLayouts(null, null, $layoutType);
        if (empty($arrLayouts)) {
            return null;
        }
        $firstLayout = $arrLayouts[0];
        return $firstLayout;
    }
    private function getLayoutsByIDs($arrLayoutIDs)
    {
        $tableLayouts = GlobalsUC::$table_layouts;
        $arrLayouts = $this->db->fetchByIDs($tableLayouts, $arrLayoutIDs);
        return $arrLayouts;
    }
    public function getCatLayouts($catID, $objAddonType = null, $onlyRecords = false)
    {
        if ($catID == "zero") {
            $catID = 0;
        }
        $arrWhere = $this->db->getWhereCatIDWithAll($catID);
        $typeName = null;
        if (!empty($objAddonType)) {
            $typeName = $objAddonType->typeName;
            if ($objAddonType->isBasicType == true) {
                $typeName = null;
            }
        }
        if ((is_numeric($catID) == false || empty($catID) || $catID === "all") && $typeName !== null) {
            $arrWhere[] = $this->db->getSqlAddonType($typeName, "layout_type");
        }
        $where = "";
        if (!empty($arrWhere)) {
            $where = implode(" and ", $arrWhere);
        }
        $records = $this->db->fetch(GlobalsUC::$table_layouts, $where, "catid, ordering");
        if ($onlyRecords === true) {
            return $records;
        }
        $arrLayouts = $this->recordsToLayouts($records);
        return $arrLayouts;
    }
    public function getNumCatLayouts($catID, UniteCreatorAddonType $objAddonType)
    {
        if ($objAddonType->isLayout == false) {
            UniteFunctionsUC::throwError("Wrong layout type");
        }
        if ($catID === 0) {
            $catID = "zero";
        }
        $arrRecords = $this->getCatLayouts($catID, $objAddonType, true);
        $numRecords = count($arrRecords);
        return $numRecords;
    }
    public function getLayoutsWithCategories($layoutType, $isShort = false)
    {
        $arrLayouts = $this->getArrLayouts(null, array(), $layoutType);
        $generated = UniteFunctionsUC::getRandomString(5);
        $prefix = "layout_" . $generated . "_";
        $arrCats = array();
        foreach ($arrLayouts as $key => $objLayout) {
            $name = $objLayout->getName();
            if (empty($name)) {
                $name = $prefix . $key;
            }
            $catTitle = $objLayout->getCategoryName();
            if (empty($catTitle)) {
                $catTitle = "Uncategorized";
            }
            if (isset($arrCats[$catTitle]) == false) {
                $arrCats[$catTitle] = array();
            }
            if ($isShort == false) {
                $arrCats[$catTitle][$name] = $objLayout;
            } else {
                $arrCats[$catTitle][$name] = $objLayout->getShortData();
            }
        }
        return $arrCats;
    }
    private function a_______OTHER_GETTERS_________()
    {
    }
    public function isLayoutExistsByTitle($title, $layoutType = null)
    {
        $arrLayout = $this->getLayoutRecordByTitle($title, $layoutType);
        $isExists = !empty($arrLayout);
        return $isExists;
    }
    private function getLayoutRecordByTitle($title, $layoutType)
    {
        $whereType = $this->db->getSqlAddonType($layoutType, "layout_type");
        $response = $this->db->fetch(GlobalsUC::$table_layouts, array("title" => $title, UniteCreatorDB::ISNULL => $whereType));
        if (empty($response)) {
            return $response;
        }
        $response = $response[0];
        return $response;
    }
    public function getMaxOrder()
    {
        $tableLayouts = GlobalsUC::$table_layouts;
        $query = "select MAX(ordering) as maxorder from {$tableLayouts}";
        $rows = $this->db->fetchSql($query);
        $maxOrder = 0;
        if (count($rows) > 0) {
            $maxOrder = $rows[0]["maxorder"];
        }
        if (!is_numeric($maxOrder)) {
            $maxOrder = 0;
        }
        return $maxOrder;
    }
    public function getUrlTakeScreenshot($isTakeScreenshot = true, $layoutID = null)
    {
        $addParams = "screenshot=true";
        if ($isTakeScreenshot == true) {
            $addParams .= "&take_screenshot=true";
        }
        $isFront = true;
        $url = HelperUC::getViewUrl_LayoutPreview($layoutID, true, $addParams, $isFront);
        return $url;
    }
    private function a_OPERATIONS()
    {
    }
    public function validateLayoutType($layoutType)
    {
        $objLayout = UniteCreatorAddonType::getAddonTypeObject($layoutType, true);
    }
    public function duplicateLayouts($arrIDs, $catID)
    {
        foreach ($arrIDs as $layoutID) {
            $layout = new UniteCreatorLayout();
            $layout->initByID($layoutID);
            $layout->duplicate();
        }
    }
    public function updateLayoutCategoryFromData($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layoutid");
        $catID = UniteFunctionsUC::getVal($data, "catid");
        $objLayout = new UniteCreatorLayout();
        $objLayout->initByID($layoutID);
        $objLayout->updateCategory($catID);
    }
    public function updateParamsFromData($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layoutid");
        $params = UniteFunctionsUC::getVal($data, "params");
        $objLayout = new UniteCreatorLayout();
        $objLayout->initByID($layoutID);
        $objLayout->updateParams($params);
        $isFromManager = UniteFunctionsUC::getVal($data, "from_manager");
        $isFromManager = UniteFunctionsUC::strToBool($isFromManager);
        if ($isFromManager == true) {
            $addonType = UniteFunctionsUC::getVal($data, "addontype");
            $objManager = UniteCreatorManager::getObjManagerByAddonType($addonType, $data);
            $htmlItem = $objManager->getAddonAdminHtml($objLayout);
            $response = array();
            $response["html_item"] = $htmlItem;
            return $response;
        }
        return array();
    }
    public function updateLayoutFromData($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layoutid");
        $isTitleOnly = UniteFunctionsUC::getVal($data, "title_only");
        $isTitleOnly = UniteFunctionsUC::strToBool($isTitleOnly);
        $objLayout = new UniteCreatorLayout();
        $isUpdate = false;
        if (empty($layoutID)) {
            $responseCreate = $objLayout->create($data);
            $layoutID = $responseCreate["id"];
            $name = $responseCreate["name"];
            $message = HelperUC::getText("layout_created");
        } else {
            $isUpdate = true;
            $objLayout->initByID($layoutID);
            $name = UniteFunctionsUC::getVal($data, "name");
            if (!empty($name)) {
                $name = HelperUC::convertTitleToAlias($name);
            } else {
                if (!isset($data["name"])) {
                    $name = null;
                }
            }
            if ($isTitleOnly == true) {
                $title = UniteFunctionsUC::getVal($data, "title");
                $objLayout->updateTitle($title, $name);
                $message = esc_html__("Title Saved", "unlimited-elements-for-elementor");
            } else {
                $objLayout->update($data);
                $message = HelperUC::getText("layout_updated");
            }
        }
        $response = array();
        $response["is_update"] = $isUpdate;
        $response["layout_id"] = $layoutID;
        if (!empty($name)) {
            $response["page_name"] = $name;
        }
        $response["message"] = $message;
        return $response;
    }
    public function deleteLayouts($arrIDs)
    {
        if (empty($arrIDs)) {
            UniteFunctionsUC::throwError("no id's to delete");
        }
        $this->db->deleteMultipleByID(GlobalsUC::$table_layouts, $arrIDs);
    }
    public function deleteLayoutFromData($data)
    {
        $objLayout = $this->getLayoutFromData($data);
        $objLayout->delete();
    }
    public function duplicateLayoutFromData($data)
    {
        $redirectToLayout = UniteFunctionsUC::getVal($data, "redirect_to_layout");
        $redirectToLayout = UniteFunctionsUC::strToBool($redirectToLayout);
        $objLayout = $this->getLayoutFromData($data);
        $duplicatedID = $objLayout->duplicate();
        $this->lastDuplicatedID = $duplicatedID;
        if ($redirectToLayout == true) {
            $urlRedirect = HelperUC::getViewUrl_Layout($duplicatedID);
            return $urlRedirect;
        }
        return null;
    }
    public function getUniqueTitle($title, $layoutType = null)
    {
        $counter = 1;
        $isExists = $this->isLayoutExistsByTitle($title, $layoutType);
        if ($isExists == false) {
            return $title;
        }
        $limit = 1;
        while ($isExists == true && $limit < 10) {
            $limit++;
            $counter++;
            $newTitle = $title . "-" . $counter;
            $isExists = $this->isLayoutExistsByTitle($newTitle, $layoutType);
        }
        return $newTitle;
    }
    public function shiftOrder($order, $layoutType)
    {
        UniteFunctionsUC::validateNumeric($order);
        $tableLayouts = GlobalsUC::$table_layouts;
        $sqlLayoutType = $this->db->getSqlAddonType($layoutType, "layout_type");
        $query = "update {$tableLayouts} set ordering = ordering+1 where ordering > {$order} and {$sqlLayoutType}";
        $this->db->runSql($query);
    }
    public function exportLayout($data = null)
    {
        if (!empty($data)) {
            $layoutID = UniteFunctionsUC::getVal($data, "id");
        } else {
            $layoutID = UniteFunctionsUC::getGetVar("id", "", UniteFunctionsUC::SANITIZE_ID);
        }
        $layoutID = (int) $layoutID;
        $objLayout = new UniteCreatorLayout();
        $objLayout->initByID($layoutID);
        $exporter = new UniteCreatorLayoutsExporter();
        $exporter->initByLayout($objLayout);
        $exporter->export();
    }
    public function importLayouts($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layoutID");
        if (!empty($layoutID)) {
            $layoutID = (int) $layoutID;
        }
        $arrTempFile = UniteFunctionsUC::getVal($_FILES, "import_layout");
        $isOverwriteAddons = UniteFunctionsUC::getVal($data, "overwrite_addons");
        $params = UniteFunctionsUC::getVal($data, "params");
        if (empty($params)) {
            $params = array();
        }
        $exporter = new UniteCreatorLayoutsExporter();
        $exporter->import($arrTempFile, $layoutID, $isOverwriteAddons, $params);
        $noRedirect = UniteFunctionsUC::getVal($data, "no_redirect");
        $noRedirect = UniteFunctionsUC::strToBool($noRedirect);
        if ($noRedirect == true) {
            return null;
        }
        if (empty($layoutID)) {
            $urlRedirect = HelperUC::getViewUrl_LayoutsList($params);
        } else {
            $urlRedirect = HelperUC::getViewUrl_Layout($layoutID, $params);
        }
        return $urlRedirect;
    }
    public function updateOrdering($layoutsIDs)
    {
        if (empty($layoutsIDs)) {
            return false;
        }
        $this->db->updateRecordsOrdering(GlobalsUC::$table_layouts, $layoutsIDs);
    }
    public function updateOrderFromData($data)
    {
        $layoutsIDs = UniteFunctionsUC::getVal($data, "layouts_order");
        $this->updateOrdering($layoutsIDs);
    }
    public function moveLayouts($arrIDs, $catID, $targetParentID = null)
    {
        $category = new UniteCreatorCategories();
        $category->validateCatExist($catID);
        foreach ($arrIDs as $layoutID) {
            $this->moveLayout($layoutID, $catID, $targetParentID);
        }
    }
    protected function moveLayout($layoutID, $catID, $targetParentID = null)
    {
        $layoutID = (int) $layoutID;
        $catID = (int) $catID;
        $arrUpdate = array();
        $arrUpdate["catid"] = $catID;
        $this->db->update(GlobalsUC::$table_layouts, $arrUpdate, array("id" => $layoutID));
    }
    public function saveSectionToLibraryFromData($data)
    {
        $dataForCreate = array();
        $title = UniteFunctionsUC::getVal($data, "section_title");
        $gridData = UniteFunctionsUC::getVal($data, "section_data");
        $dataForCreate["title"] = $title;
        $dataForCreate["grid_data"] = $gridData;
        $layoutType = GlobalsUC::ADDON_TYPE_LAYOUT_SECTION;
        $dataForCreate["layout_type"] = $layoutType;
        $isOverwrite = UniteFunctionsUC::getVal($data, "section_overwrite");
        $isOverwrite = UniteFunctionsUC::strToBool($isOverwrite);
        $objLayout = new UniteCreatorLayout();
        $record = null;
        if ($isOverwrite == true) {
            $record = $this->getLayoutRecordByTitle($title, $layoutType);
        }
        if ($record) {
            $objLayout->initByRecord($record);
            $objLayout->updateGridData($gridData);
            $layoutID = $objLayout->getID();
        } else {
            $catID = UniteFunctionsUC::getVal($data, "section_library_category");
            $dataForCreate["catid"] = $catID;
            if ($dataForCreate["catid"] == "new" || is_numeric($catID) == false || empty($catID)) {
                $newCatTitle = UniteFunctionsUC::getVal($data, "section_category_new");
                $objCat = new UniteCreatorCategory();
                $catCreateResponse = $objCat->add($newCatTitle, $layoutType);
                $newCatID = $catCreateResponse["id"];
                $dataForCreate["catid"] = $newCatID;
            }
            $createResponse = $objLayout->create($dataForCreate);
            $layoutID = $createResponse["id"];
        }
        $response = array();
        $response["layoutid"] = $layoutID;
        return $response;
    }
    public function getLayoutGridDataForEditor($data)
    {
        $layoutID = UniteFunctionsUC::getVal($data, "layoutid");
        $objLayout = new UniteCreatorLayout();
        $objLayout->initByID($layoutID);
        $gridData = $objLayout->getGridDataForEditor();
        $gridData = json_encode($gridData);
        $response = array();
        $response["grid_data"] = $gridData;
        return $response;
    }
}