ElixirVault/www/system-brs-core/cms/cmsTextManager/inc/cmsTextManagerController.php

1151 lines
44 KiB
PHP

<?php
namespace BRSystems\Core\Cms\cmsTextManager\inc;
use BRSystems\Core\Cms\cmsClassificators\inc\cmsClassificatorsModule;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cDefuseCrypto\cDefuseCrypto;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cMapUrls\cMapUrls;
use BRSystems\Core\Library\cApc\cCacheHelper;
use BRSystems\Core\Library\cValidator\cValidator;
use BRSystems\Core\Sys\sysLanguages\inc\sysLanguagesModule;
class cmsTextManagerController extends cCore
{
public string $dir;
public string $name;
public cmsTextManagerModule $module;
/**
* CFG for image upload.
* preview_size and cuts are WxH
*/
static array $_cfg = [
'img' => [
'upload_dir' => '/files/article',
'preview_dir' => 'preview',
'img_type' => 'textManager',
'preview_size' => '134x100',
'cut' => [
'service_card' => '80x80',
'preview' => '134x100',
'base_cat' => '450x550',
'sub_cat' => '512x512',
'subscribe' => '550x200',
'category_bg' => '1920x150',
'testimonial' => '1920x540',
'carousel' => '1920x950',
'shop_side_banner' => '360x420',
'collection_banner' => '1420x330',
],
],
'file' => [
'upload_dir' => '/files/article/files',
'preview_dir' => 'preview',
'img_type' => 'textManager',
'preview_size' => '134x100', // TODO: Do we really need a preview size for a file?
],
];
static array $imageExtensions = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'svg'];
static array $previewExtensions = ['gif', 'jpeg', 'JPEG', 'jpg', 'JPG', 'png', 'PNG'];
static array $fileExtensions = [
'rar',
'zip',
'doc',
'docx',
'pdf',
'xls',
'xlsx',
'odt',
'ods',
'odp',
'ppt',
'pptx',
];
static array $lvSpecialChars = [
'Ā' => 'A',
'ā' => 'a',
'Č' => 'C',
'č' => 'c',
'Ē' => 'E',
'ē' => 'e',
'Ģ' => 'G',
'ģ' => 'g',
'Ī' => 'I',
'ī' => 'i',
'Ķ' => 'K',
'ķ' => 'k',
'Ļ' => 'L',
'ļ' => 'l',
'Ņ' => 'N',
'ņ' => 'n',
'Ū' => 'U',
'ū' => 'u',
'Š' => 'S',
'š' => 's',
'Ž' => 'Z',
'ž' => 'z',
];
static array $pagesArray = [
//cmsTextManagerConst::PAGE_TYPE_INDEX => 364,
//cmsTextManagerConst::PAGE_TYPE_SHOP => 364,
//cmsTextManagerConst::PAGE_TYPE_CART => 364,
//cmsTextManagerConst::PAGE_TYPE_COOKIES => 364,
];
public function __construct($uriData, $dir, $name)
{
$this->dir = $dir ?? '';
$this->name = $name ?? '';
parent:: __construct($uriData);
$this->module = new cmsTextManagerModule();
$this->oView->setTemplateDir($this->dir . "/tmpl/");
if (!$this->oView->cacheUsed()) {
$this->oView->setJs(getCurrentFolder($this->dir) . '/js/' . $this->name . '.js');
$this->oView->setCss(getCurrentFolder($this->dir) . '/css/' . $this->name . '.css');
}
// Set JS module init title
$this->oView->setAsJSON(cCore::JS_MOD, ucfirst($name));
}
/**
* Function to show main form in the module
*
* @category cmsTextManager Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @returns bool true
*/
public function run($initEditMode = false)
{
$this->oView->TABLE = true;
$this->oView->setAsJSON('initEdit', $initEditMode);
$this->oView->setAsJSON('baseUrl', $this->gL());
$this->oView->setAsJSON('gM', $this->getTranslations());
$this->oView->setAsJSON('vcfg', $this->getValCfg());
$this->oView->setAsJSON('uploadImg', $this->getUploadConfig());
$this->oView->setAsJSON('uploadFile', $this->getUploadConfig(true));
$this->oView->setAsJSON('prevExt', self::$previewExtensions);
$this->oView->setAsJSON('oShow', $this->createTypeCfg());
$this->oView->display();
}
/**
* Function to create cms table & return for direct & ajax input
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param int $page - page to display
* @param string $filter_json - (optional) filter json string
* @param bool $headers - flag whether to send header data or not
* @return array of data
* @version 1.1
*/
public function createCmsTable($page = 1, $filter_json = '', $headers = false) {
$pages = 0;
$filter = array();
if($filter_json){
$filter = cJSON::decode($filter_json, true);
}
$this->oView->MODULE_TITLE = $this->gM("Text manager");
if(!!$filter && is_array($filter))
$this->oView->DEFAULT_SEARCH_VALUES = prepForDisplayHtml($filter);
$this->oView->LIST_HEADER = reOrderTableFields($this->getTableFields(), $this->oUser->oCfg->getTableProperty($this->name));
$this->oView->LIST_DATA = $this->getModuleData($page, $filter, $pages);
$this->oView->LIST_ACTIONS = $this->getTableActions();
$data = array(
'data' => $this->oView->genHelperView('cmsc/table/data-table'),
'pcount' => $pages,
'rlimit' => $this->cmsTableRecordsPerPage
);
// Send data to client
if($headers)
$data['header'] = $this->oView->genHelperView('cmsc/table/data-table-search');
return $data;
}
/**
* Function to get module data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param int $page
* @param array $filter
* @param int $pages
* @return array of data
* @version 1.1
*/
public function getModuleData($page = 1, $filter = array(), &$pages) {
$where = "AND 1";
$sort = "ma.id DESC";
$offset = (($page?$page:1)-1) * $this->cmsTableRecordsPerPage;
$tFields = $this->getTableFields();
// Create sort by
if(getP('sortBy'))
$sort = $this->module->db->genOrderByQuery(getP('sortBy'), getP('sortWay'), $tFields);
// Create where clause
if(count($filter))
$where = $this->module->db->genSearchFieldQuery($filter, $tFields);
$subWhere = "";
$data = $this->module->getModuleRecords($where . $subWhere, $sort, $offset, $this->cmsTableRecordsPerPage, cmsTextManagerConst::DEFAULT_LANGUAGE);
$this->oView->ROWS = $rows = $this->module->db->getSelectedRecordsCount();
$pages = floor($rows / $this->cmsTableRecordsPerPage) + ($rows%$this->cmsTableRecordsPerPage?1:0);
if(!!$data){
foreach($data as &$val){
if($val['a_created'])
$val['a_created'] = date($this->CMSDateFormat, $val['a_created']);
if(!$val['a_title'])
$val['a_title'] = '-';
$val['a_title'] = "<span class='icon_link'></span>" . $val['a_title'];
}
unset($val);
}
return $data;
}
/**
* Function to disable / enable records marked in table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsProductAttributes Controller
* @param array $baseField - base field to handle
* @param string $rows - passed user ids in JSON string
* @return array of data
*/
public function changeEnumFields($baseField, $rows) {
if($baseField && $rows = cJSON::decode($rows, true)){
$baseField = substr($baseField, 2);
$fields = $this->getTableFields();
if(isset($fields[$baseField]['db_field'])){
$rows = $this->module->db->prepData($rows);
if($this->module->changeRecordsStatus($rows, $fields[$baseField]['db_field'])){
$this->dropTextManagerApcKeys($this->module->getTypeByIds($rows)); // Drop apc keys
return true;
}
}
}
cJSON::sendError();
}
/**
* Function to delete records marked in table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param string $rows - passed user ids in JSON string
* @return array of data
*/
public function delete($rows) {
if($rows = cJSON::decode($rows, true)){
$rows = $this->module->db->prepData($rows);
$types = $this->module->getTypeByIds($rows);
$imgToDelete = $this->module->deleteRecords($rows);
if(!!$imgToDelete){
// Remove each image
if(is_array($imgToDelete))
foreach($imgToDelete as $file)
unlink(__MDIR__ . $file);
$this->dropTextManagerApcKeys($types); // Drop apc keys
return true;
}else{
cJSON::sendError();
}
}
}
/**
* Function to get link
*
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param int $id - record id
* @return array
*/
public function getLinkToArticle($id)
{
$type = $this->module->getArticleTypeById($id);
if (!empty($type['a_type']) && !empty(self::$pagesArray[$type['a_type']])) {
$endparam = 0 ? "jumpto:a{$id}" : "";
$link = $this->getLinkForID(
self::$pagesArray[$type['a_type']],
cmsTextManagerConst::DEFAULT_LANGUAGE,
false,
$endparam,
);
return ['link' => $link, 'title' => $type['a_title']];
}
cJSON::sendError();
}
/**
* Function to get table record data for edit mode
*
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param int $id - record id
* @return array of data
*/
public function editCmsTableRecord($id): array
{
$data = [
'files' => [
'img' => [],
'car_img' => [],
'cnt_files' => [],
],
'translations' => [],
];
if ($id) {
// Get base data and translations and set them to view
$data = $data + $this->module->getRecordBaseDataById($id);
if (!!$data['a_date'])
$data['a_date'] = date('d/m/Y', $data['a_date']);
$data['translations'] = $this->module->getRecordTranslateDataById($id);
if (date('d-m-Y', $data['a_created']) == date('d-m-Y')) {
$data['a_created'] = date('H:i', $data['a_created']);
} else {
$data['a_created'] = date($this->CMSDateFormat, $data['a_created']);
}
if (date('d-m-Y', $data['a_modified']) == date('d-m-Y')) {
$data['a_modified'] = date('H:i', $data['a_modified']);
} else {
$data['a_modified'] = date($this->CMSDateFormat, $data['a_modified']);
}
// Set data to view
$this->oView->DATA = $data;
// Get all image data
$images = $this->module->getImagesData($id);
if (!!$images && is_array($images)) {
// Set background images if any
if(!empty($images[cmsTextManagerConst::IMG_TYPE]) && is_array($images[cmsTextManagerConst::IMG_TYPE])){
foreach($images[cmsTextManagerConst::IMG_TYPE] as $v){
$data['files']['img'][$v['file_order']] = array(
'img_id' => $v['id'],
'img_blocked' => $v['file_disabled'],
'img_name' => $v['file_name'],
'img_path' => $v['file_link'],
'img_ppath' => dirname($v['file_link']) . '/' . cmsTextManagerController::$_cfg['img']['preview_dir'] . '/' . basename($v['file_link']),
);
// Exclude svg images
if(getFileExtension(basename($v['file_link'])) == 'svg'){
$data['files']['img'][$v['file_order']]['img_ppath'] = $v['file_link'];
}
}
}
// Set carousel images if any
if(!empty($images[cmsTextManagerConst::IMG_CAROUSEL_TYPE]) && is_array($images[cmsTextManagerConst::IMG_CAROUSEL_TYPE])){
foreach($images[cmsTextManagerConst::IMG_CAROUSEL_TYPE] as $v){
$data['files']['car_img'][$v['file_order']] = array(
'img_id' => $v['id'],
'img_blocked' => $v['file_disabled'],
'img_name' => $v['file_name'],
'img_path' => $v['file_link'],
'img_ppath' => dirname($v['file_link']) . '/' . cmsTextManagerController::$_cfg['img']['preview_dir'] . '/' . basename($v['file_link']),
);
}
}
// Set content images if any
if(!empty($images[cmsTextManagerConst::IMG_CNT_TYPE]) && is_array($images[cmsTextManagerConst::IMG_CNT_TYPE])){
foreach($images[cmsTextManagerConst::IMG_CNT_TYPE] as $v){
$data['files']['cnt_files'][$v['file_order']] = array(
'img_id' => $v['id'],
'img_blocked' => $v['file_disabled'],
'img_name' => $v['file_name'],
'img_path' => $v['file_link'],
'img_ppath' => dirname($v['file_link']) . '/' . cmsTextManagerController::$_cfg['img']['preview_dir'] . '/' . basename($v['file_link']),
);
}
}
}
}
$cat = $this->module->getCategories([cmsTextManagerConst::BASE_CATEGORIES]);
$subCat = $this->module->getCategories(cmsTextManagerConst::CATEGORY_TYPES, true);
foreach ($subCat as &$v) {
if (!empty($cat[$v['a_cat']])) {
$v['title'] = $cat[$v['a_cat']]['title'] . " - " . $v['title'];
}
}
unset($v);
$this->oView->CAT = $cat;
$this->oView->SUBCAT = $subCat;
$this->oView->SVG = $this->module->getTypes(cmsClassificatorsModule::CL_SVG_ICONS);
$this->oView->TYPES = $this->module->getTypes(cmsTextManagerConst::CL_TYPE_ARTICLES);
$this->oView->lang = (new sysLanguagesModule())->getLanguages(true, true);
return [
'show' => ['buttons_top' => false, 'buttons_bottom' => true],
'btn' => $this->getFormButtons(),
'files' => $data['files'],
'bNew' => (!$id ? true : false),
'html' => $this->oView->genTemplate('textManager'),
];
}
/**
* Function to save Record data
*
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param int $recordId - record id
* @param string $fields - param JSON string
* @return array of data
*/
public function saveRecordData($recordId, $fields)
{
$fields = $this->getSubmittedDataAsArray($fields ?? '');
if (empty($fields) || !is_array($fields)) {
cJSON::sendError($this->gM('Nothing to save!'));
}
// Validate fields
$errors = (new cValidator($this))->checkArray($fields, $this->getValCfg());
if ($errors !== true) cJSON::sendError(['data' => $errors]);
// Create base data array
$update = [
'a_disabled' => (!empty($fields['psettings']['a_disabled']) ? 1 : 0),
'a_npk' => $fields['pbase']['a_npk'] ?: 0,
'a_type' => $fields['pbase']['a_type'],
'a_cl_id' => (!empty($fields['pbase']['a_cl_id']) ? $fields['pbase']['a_cl_id'] : 0),
'a_cat' => 0,
'a_unique_name' => (!empty($fields['pbase']['a_unique_name']) ? $fields['pbase']['a_unique_name'] : ''),
'a_link' => (!empty($fields['pbase']['m_y_link']) ? $fields['pbase']['m_y_link'] : ''),
'a_title' => (!empty($fields['pbase']['a_title']) ? $fields['pbase']['a_title'] : ''),
'a_date' => (!empty($fields['pbase']['a_date']) ? $fields['pbase']['a_date'] : 0),
'a_modified' => time(),
];
if (!$recordId) $update['a_created'] = time();
if (!empty($fields['pbase']['a_cat']))
$update['a_cat'] = $fields['pbase']['a_cat'];
if (!empty($fields['pbase']['a_sub_cat']))
$update['a_cat'] = substr($fields['pbase']['a_sub_cat'], 2);
//---------------------------------------------------------
// Get article record data
//---------------------------------------------------------
$urls4products = [];
$articleRecordData = [];
$apcToDrop = [];
foreach ($fields['article'] as $key => $val) {
$langId = substr($key, 1);
// Url to lower
$link = stripSpecialChars(strtolower($val["a_link-{$langId}"]), ["-"], false);
if (strlen($link) && in_array($update['a_type'], cmsTextManagerConst::CATEGORY_TYPES, true)) {
// If url segment is in use send an error
if ($this->module->chkUrlSegment($link, $recordId, $langId))
cJSON::sendError(['msg' => "Url segment already in use, please chose a different one"]);
// Create array of data for url construction
$urls4products[$langId] = ['l' => $link];
}
// Create record data set
$articleRecordData[$langId] = [
'a_id' => &$recordId,
'a_lang_id' => $langId,
'a_shtml' => (!empty($val["a_srecord-{$langId}"]) ? $val["a_srecord-{$langId}"] : ''),
'a_lhtml' => (!empty($val["a_record-{$langId}"]) ? $val["a_record-{$langId}"] : ''),
'a_title' => $val["a_title-{$langId}"],
'a_link' => $link,
'a_v' => '',
'a_url_alias_id' => 0,
];
if (!empty($val["a_btn_text-{$langId}"]))
$articleRecordData[$langId]['a_v'] = $val["a_btn_text-{$langId}"];
}
//---------------------------------------------------------
// Check & create site map full alias urls
//---------------------------------------------------------
$oMapU = false;
if (!!$urls4products && is_array($urls4products)) {
$oMapU = new cMapUrls();
if ($update['a_type'] == cmsTextManagerConst::BASE_CATEGORIES) {
$errors = $oMapU->checkAndCreateMapUrlsForInsert(
$urls4products,
$recordId,
cmsTextManagerConst::CATEGORY_ALIAS_ID,
false,
cmsTextManagerConst::MAP_URL_TYPE,
);
} else {
if ($update['a_type'] == cmsTextManagerConst::BASE_SUB_CATEGORIES) {
$t = $this->module->getAliasRefIdById($update['a_cat']);
$errors = $oMapU->checkAndCreateMapUrlsForInsert(
$urls4products,
$recordId,
false,
$t,
cmsTextManagerConst::MAP_SUB_URL_TYPE,
3,
);
}
}
// Send url array errors
if (!!$errors && is_array($errors)) {
foreach ($errors as $langId => $v) {
$errors["a_link-seg-{$langId}"] = $this->gM('Url already in use!');
unset($errors[$langId]);
}
// Send errors
cJSON::sendError(['data' => $errors]);
}
}
//---------------------------------------------------------
// Get all article images data
//---------------------------------------------------------
$ii = 0;
$galleryDataUpdate = [];
$galleryDataInsert = [];
$bDeleteImages = false;
if (!!$fields["cnt_img"] && is_array($fields["cnt_img"])) {
$bDeleteImages = true;
foreach ($fields["cnt_img"] as $key => $val) {
// Check & save only if file exists
if($val["img"] && file_exists(__MDIR__ . substr($val["img"], 1))){
$i = substr($key, 5);
$row = array(
"ref_id" => &$recordId,
"file_name" => $val["img_name"],
"file_link" => $val["img"],
"file_disabled" => ($val["img_disabled"] ? 1 : 0),
"file_order" => $ii,
"file_type" => cmsTextManagerConst::IMG_TYPE,
);
if(!!$val["img_id"] && $val["img_id"] > 0){
$row["id"] = $val["img_id"];
$galleryDataUpdate[] = $row;
}else{
$galleryDataInsert[] = $row;
}
$ii++;
}
}
}
$ii = 0;
if (isset($fields["car_img"]) && is_array($fields["car_img"])) {
$bDeleteImages = true;
foreach($fields["car_img"] as $key => $val){
// Check & save only if file exists
if($val["img"] && file_exists(__MDIR__ . substr($val["img"], 1))){
// $i = substr($key, 5);
$row = array(
"ref_id" => &$recordId,
"file_name" => $val["img_name"],
"file_link" => $val["img"],
"file_disabled" => ($val["img_disabled"] ? 1 : 0),
"file_order" => $ii,
"file_type" => cmsTextManagerConst::IMG_CAROUSEL_TYPE,
);
if(!!$val["img_id"] && $val["img_id"] > 0){
$row["id"] = $val["img_id"];
$galleryDataUpdate[] = $row;
}else{
$galleryDataInsert[] = $row;
}
$ii++;
}
}
}
$ii = 0;
if (!!$fields["cnt_files"] && is_array($fields["cnt_files"])) {
$bDeleteImages = true;
foreach($fields["cnt_files"] as $key => $val){
// Check & save only if file exists
if(!empty($val["img"]) && file_exists(__MDIR__ . substr($val["img"], 1))){
// $i = substr($key, 5);
$row = array(
"ref_id" => &$recordId,
"file_name" => $val["img_name"],
"file_link" => $val["img"],
"file_disabled" => (!empty($val["img_disabled"]) ? 1 : 0),
"file_order" => $ii,
"file_type" => cmsTextManagerConst::IMG_CNT_TYPE,
);
if(!!$val["img_id"] && $val["img_id"] > 0){
$row["id"] = $val["img_id"];
$galleryDataUpdate[] = $row;
}else{
$galleryDataInsert[] = $row;
}
$ii++;
}
}
}
// Update / Insert record data
$oldId = $recordId;
if ($recordId = $this->module->saveRecordDataById($recordId, $update)) {
// Init map url Update
// Init map url Update
if ($oMapU && is_callable(array($oMapU, 'updateMapUrls')))
$oMapU->updateMapUrls($articleRecordData, 'a_url_alias_id', true);
// Save article record data
if (!!$articleRecordData && is_array($articleRecordData))
$this->module->saveRecordTextDataById($recordId, $articleRecordData);
// Save article image data
$this->module->saveRecordedImg($recordId, $galleryDataInsert, $galleryDataUpdate, $bDeleteImages);
// Update npk by id f none given
if (empty($update['a_npk']))
$this->module->saveRecordDataById($recordId, array('a_npk' => $recordId));
if (in_array($update['a_type'], cmsTextManagerConst::CATEGORY_TYPES, true)) {
$this->dropCategoriesCacheKeys();
}
// Drop apc by type
$this->dropTextManagerApcKeys($update['a_type']);
// Drop prod apc keys if any
if (!!$apcToDrop) $this->oApc->setExpired($apcToDrop);
} else {
cJSON::sendError(['error' => 'Something went wrong saving base data!']);
}
cJSON::sendOk($oldId ? "" : ['newid' => $recordId]);
}
/**
* Function to create table headers + search
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @return array of data
* @version 1.1
*/
public function getTableFields() {
if(!$this->oUser->chkPermission('CMS_TEXTMANAGER_SHOW_MANAGEMENT', false, false))
return false;
$statusData = array(
'' => array(
'title' => $this->gM("Select")
),
0 => array(
'title' => $this->gM("Active")
),
1 => array(
'title' => $this->gM("Inactive")
)
);
$types = $this->module->getTypes(cmsTextManagerConst::CL_TYPE_ARTICLES);
$aTypes = ['' => ['title' => $this->gM("-- Select --")]] + $types;
$tFields = array(
"checkbox" => array(
'sort' => false,
'title' => '',
'class' => 'w4 center rchk'
),
"a_npk" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("NPK"),
'class' => 'w6 center',
'db_field' => 'ma.a_npk',
'type' => 'input',
),
"id" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("ID"),
'class' => 'w7 center sortdesc',
'db_field' => 'ma.id',
'type' => 'input',
),
"a_title" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("Article Title"),
'class' => 'w32 sClass',
'db_field' => 'mar.a_title',
'type' => 'input',
),
/*"a_record" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("Article description"),
'class' => 'w32',
'db_field' => 'mar.a_record',
'type' => 'input',
),*/
"cl_name" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Type"),
'class' => 'w22',
'db_field' => 'a_type',
'type' => 'dropBox',
'data' => $aTypes,
'default' => '',
),
"a_created" => array(
'sort' => true,
'search' => false,
'title' => $this->gM("Article created"),
'class' => 'w12 center',
'db_field' => 'ma.a_created',
'type' => 'input',
),
"status" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Disabled"),
'class' => 'w7 center',
'field' => 'a_disabled',
'db_field' => 'ma.a_disabled',
'type' => 'dropBox',
'data' => $statusData,
'default' => '',
),
"buttons" => array(
'sort' => false,
'class' => 'w30',
'title' => $this->gM("Actions"),
'btnset' => array(
'ledit' => array(
'edit' => true,
'title' => $this->gM("Edit"),
),
'ldelete' => array(
'title' => $this->gM("Delete"),
)
)
)
);
if(!$this->chkP('CMS_TEXTMANAGER_EDIT')){
unset($tFields['buttons']['btnset']['ledit']);
unset($tFields['buttons']['btnset']['ldelete']);
}
if(!$this->chkP('SYS_SHOW_TABLE_IDS'))
unset($tFields['id']);
return $tFields;
}
/**
* Function to get js translations for view
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @return array of data
*/
public function getTranslations(){
return array(
'yes' => $this->gM('Yes'),
'no' => $this->gM('No'),
'delTitle' => $this->gM('Delete article?'),
'delMessage' => $this->gM('Selected article will be permanently deleted and cannot be recovered. Confirm delete?'),
'buttonYes' => $this->gM('Delete selected article'),
'buttonCancel' => $this->gM('Cancel operation'),
);
}
/**
* Function to get table actions
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @return array of data
*/
public function getTableActions(){
$buttons = array(
'inew' => array(
'lbl' => $this->gM("Add new entry"),
'icon' => 'ico-addnew',
),
'istatus' => array(
'lbl' => $this->gM("Change item status"),
'sepline' => true,
),
'idelete' => array(
'lbl' => $this->gM("Delete selected"),
'icon' => 'ico-delete2',
'sepline' => true,
),
);
return $buttons;
}
/**
* Function to get js validator cfg
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsProductAttributes Controller
* @return array of data
*/
public function getValCfg(){
// Create validation array
$lng = new sysLanguagesModule();
$lang = $lng->getLanguages(true, true);
$data = array(
'a_type' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_EVENT => 'change',
cValidator::P_TEST => array(cValidator::CHK_NUM), // ?? or string?
),
'a_date' => array(
'type' => cValidator::TY_DATE,
cValidator::P_COMP => 0,
cValidator::P_EVENT => 'change',
cValidator::P_ADDS => array('2unix' => true),
cValidator::P_TEST => array(cValidator::CHK_DATE),
),
'a_npk' => array(
cValidator::P_TYPE => cValidator::TY_DEC,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_NUM),
),
'a_unique_name' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_TEST => array(cValidator::CHK_URL_SEGM),
),
'm_y_link' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
),
);
foreach($lang as $v){
$data["a_title-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
$data["a_link-full-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
$data["a_link-seg-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_TEST => array(cValidator::CHK_URL_SEGM),
);
$data["a_link-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
$data["a_btn_text-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
$data["a_srecord-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
$data["a_record-{$v['id']}"] = array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
);
}
return $data;
}
/**
* Function to get form buttons
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @return array of data
*/
public function getFormButtons(){
return array(
1 => array(
'id' => 'fsave',
'lbl' => $this->gM('Save')
),
2 => array(
'id' => 'fcancel',
'lbl' => $this->gM('Cancel')
)
);
}
/**
* Function to get uploadify config
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category CMS productEdit
* @param bool $file - if its file or not
* @return array of data
*/
public function getUploadConfig($file = false): array
{
$encrypt = new cDefuseCrypto();
return [
"script" => "/js/fileupload/php/",
"buttonText" => $this->gM('Click to add'),
"scriptData" => [
'dxcheck' => $encrypt->encrypt(session_id()),
'upload' => $encrypt->encrypt(__CLASS__ . ($file ? "::getFileUploadConfig" : "::getImgUploadConfig")),
],
];
}
public static function getImgUploadConfig(): array
{
// TODO: Ko darīt ar content failiem?
$typeCuts = [
cmsTextManagerConst::INDEX_PAGE_HERO_SLIDER => ['carousel'],
cmsTextManagerConst::BASE_CATEGORIES => ['base_cat', 'category_bg'],
cmsTextManagerConst::INDEX_PAGE_SERVICES => ['service_card'],
cmsTextManagerConst::INDEX_PAGE_TESTIMONIAL_BG => ['testimonial'],
cmsTextManagerConst::POPUP_SUBSCRIBE_BG => ['subscribe'],
cmsTextManagerConst::GLOBAL_BREADCRUMBS => ['category_bg'],
cmsTextManagerConst::SHOP_SIDE_BANNER => ['shop_side_banner'],
cmsTextManagerConst::COLLECTION_BANNER => ['collection_banner'],
];
return [
"fileExt" => implode('|', self::$imageExtensions),
"slices_extensions" => self::$previewExtensions,
"preview_extensions" => self::$previewExtensions,
"sizeLimit" => 6291456,
'cms' => 1,
'type_cuts' => $typeCuts,
'slices' => self::$_cfg['img']['cut'],
'folder' => self::$_cfg['img']['upload_dir'],
'preview_dir' => self::$_cfg['img']['preview_dir'],
'preview_size' => self::$_cfg['img']['preview_size'],
'separate' => 1,
];
}
public static function getFileUploadConfig(): array
{
return [
"fileExt" => implode('|', array_merge(self::$imageExtensions, self::$fileExtensions)),
"preview_extensions" => self::$previewExtensions,
"sizeLimit" => 12291456,
'cms' => 1,
'folder' => self::$_cfg['file']['upload_dir'],
'preview_dir' => self::$_cfg['file']['preview_dir'],
'preview_size' => self::$_cfg['file']['preview_size'],
'separate' => 1,
'saveFileName' => 1,
];
}
public static function createTypeCfg(): array
{
// Create array for selectors
$record = 'div.a_record';
$shortRecord = 'input[id^="a_srecord-"]';
$link = 'input[id^="a_link-"]';
$btnText = 'input[id^="a_btn_text-"]';
$cntFiles = 'div.cnt_files_settings';
$image = 'div.img_settings';
// Defaults as arrays (easier to merge & dedupe)
$defaultTextOnly = [$record];
$defaultImageOnly = [$image];
$defaultTextWithImage = [$record, $image];
$defaultTextWithFiles = [$record, $cntFiles];
$defaultShortRecord = [$shortRecord];
// Define configuration using arrays of selectors
$oShow = [
// Index page types
cmsTextManagerConst::INDEX_PAGE_HERO_SLIDER => [$image, $link, $shortRecord, $btnText],
cmsTextManagerConst::INDEX_PAGE_SERVICES => [$shortRecord, $image],
cmsTextManagerConst::INDEX_PAGE_ABOUT_PRODUCT => $defaultShortRecord,
cmsTextManagerConst::INDEX_PAGE_TESTIMONIAL => [$shortRecord, $record, $image],
cmsTextManagerConst::INDEX_PAGE_TESTIMONIAL_BG => $defaultImageOnly,
cmsTextManagerConst::INDEX_PAGE_ABOUT_TEXT => [$shortRecord, $record, $btnText, $link],
// Other pages
cmsTextManagerConst::PAGE_TYPE_AGREE_TO_TERMS => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_CONTACT_US => $defaultShortRecord,
cmsTextManagerConst::PAGE_TYPE_COOKIES => $defaultTextOnly,
cmsTextManagerConst::POPUP_SUBSCRIBE_BG => $defaultImageOnly,
cmsTextManagerConst::SHOP_SIDE_BANNER => [$image, $link],
cmsTextManagerConst::COLLECTION_BANNER => $defaultImageOnly,
cmsTextManagerConst::PAGE_TYPE_ABOUT_US => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_FAQ => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_DISCLAIMER => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_PRIVACY_POLICY => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_PAYMENT_POLICY => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_RETURN_POLICY => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_SHIPPING_POLICY => $defaultTextWithFiles,
cmsTextManagerConst::PAGE_TYPE_COPYRIGHT => $defaultTextWithFiles,
// Category types
cmsTextManagerConst::BASE_CATEGORIES => [$image, $link],
cmsTextManagerConst::BASE_SUB_CATEGORIES => [$image, $link, '#a_cat'],
];
// Convert to expected format and collect all unique selectors
$allSelectors = ['#a_cl_id'];
foreach ($oShow as $key => $selectors) {
$oShow[$key] = ['s' => implode(', ', $selectors)];
$allSelectors = array_merge($allSelectors, $selectors);
}
$oShow['all'] = implode(',', array_unique($allSelectors));
return $oShow;
}
/**
* Function to get link id for file & send it encrypted, except for embed images
*
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
* @category cmsTextManager Controller
* @param string $file_url - file path
* @param int $file_id - file id
* @return array string
*/
public function getLinkToFile($file_url, $file_id){
$cry = new cDefuseCrypto();
if(!!$file_id){
return array('id'=> $file_id, 'link' => '/a:file/f:'. $cry->encrypt($file_id));
}else if(!!$file_url){
$id = $this->module->insertTemporallyFile($file_url);
return array('id'=> $id, 'link' => '/action:file/f:'. $cry->encrypt($id));
}
return false;
}
public function dropTextManagerApcKeys($type_id): bool
{
$type = (array)$type_id;
if (empty($type)) {
return false;
}
$lng = (new sysLanguagesModule())->getLanguages(true, true);
$cache = new cCacheHelper($this->oApc);
foreach ($type as $v) {
$apcKey = "";
switch ($v) {
case cmsTextManagerConst::INDEX_PAGE_HERO_SLIDER:
case cmsTextManagerConst::INDEX_PAGE_SERVICES:
case cmsTextManagerConst::INDEX_PAGE_ABOUT_PRODUCT:
case cmsTextManagerConst::INDEX_PAGE_TESTIMONIAL:
case cmsTextManagerConst::INDEX_PAGE_TESTIMONIAL_BG:
case cmsTextManagerConst::INDEX_PAGE_ABOUT_TEXT:
case cmsTextManagerConst::GLOBAL_BREADCRUMBS:
$apcKey = cmsTextManagerConst::APC_INDEX_GLOBAL_KEY;
break;
case cmsTextManagerConst::PAGE_TYPE_COOKIES:
$apcKey = cmsTextManagerConst::APC_INDEX_COOKIES_KEY;
break;
case cmsTextManagerConst::PAGE_TYPE_AGREE_TO_TERMS:
$apcKey = cmsTextManagerConst::APC_INDEX_AGREE_TO_TERMS_KEY;
break;
case cmsTextManagerConst::PAGE_TYPE_CONTACT_US:
$apcKey = cmsTextManagerConst::APC_INDEX_CONTACT_US_KEY;
break;
case cmsTextManagerConst::PAGE_TYPE_DISCLAIMER:
case cmsTextManagerConst::PAGE_TYPE_COPYRIGHT:
case cmsTextManagerConst::PAGE_TYPE_FAQ:
$apcKey = $v;
break;
case cmsTextManagerConst::SHOP_SIDE_BANNER:
$apcKey = cmsTextManagerConst::APC_SHOP_SIDE_BANNER_KEY;
break;
case cmsTextManagerConst::COLLECTION_BANNER:
$apcKey = cmsTextManagerConst::APC_COLLECTION_BANNER_KEY;
break;
case cmsTextManagerConst::BASE_CATEGORIES:
case cmsTextManagerConst::BASE_SUB_CATEGORIES:
$apcKey = cmsTextManagerConst::APC_CATEGORY_MENU_KEY;
break;
}
if ($apcKey !== "") {
foreach ($lng as $vv) {
$cache->delete($apcKey, $vv['id']);
}
if (in_array($v, cmsTextManagerConst::CATEGORY_TYPES)) {
foreach ($lng as $vv) {
$cache->delete(cmsTextManagerConst::APC_HEADER_CATEGORY_MENU_KEY, $vv['id']);
}
}
}
}
return true;
}
private function dropCategoriesCacheKeys()
{
$cache = new cCacheHelper($this->oApc);
$langIds = (new sysLanguagesModule())->getLanguages(true, true);
foreach ($langIds as $lang) {
$cache->delete(cmsTextManagerConst::APC_INDEX_GLOBAL_KEY, $lang['id']);
$cache->delete(cmsTextManagerConst::BASE_CATEGORIES, $lang['id']);
$cache->delete(cmsTextManagerConst::CATEGORY_TYPES, $lang['id']);
}
}
}