#5 | Phalcon\Mvc\Model::find
/var/www/html/discounts-n-coupons.com/src/app/Lib/Mvc/Model/UcmItem.php (462) <?php
namespace CLSystems\PhalCMS\Lib\Mvc\Model;
use Phalcon\Db\Adapter\Pdo\Mysql;
use CLSystems\PhalCMS\Lib\Helper\Event as EventHelper;
use CLSystems\PhalCMS\Lib\Helper\UcmItem as UcmItemHelper;
use CLSystems\PhalCMS\Lib\Helper\StringHelper;
use CLSystems\PhalCMS\Lib\Helper\Uri;
use CLSystems\PhalCMS\Lib\Helper\Language;
use CLSystems\PhalCMS\Lib\Form\FormsManager;
use CLSystems\PhalCMS\Lib\Form\Form;
use CLSystems\PhalCMS\Lib\Factory;
use CLSystems\Php\Registry;
use CLSystems\Php\Filter;
use Phalcon\Mvc\Model\ResultsetInterface;
class UcmItem extends ModelBase
{
/**
*
* @var integer
*/
public $id;
/**
*
* @var integer
*/
public $parentId;
/**
*
* @var string
*/
public $context;
/**
*
* @var string
*/
public $title;
/**
*
* @var string
*/
public $route;
/**
*
* @var integer
*/
public $state;
/**
*
* @var string
*/
public $image;
/**
*
* @var string
*/
public $summary;
/**
*
* @var string
*/
public $description;
/**
*
* @var string
*/
public $createdAt;
/**
*
* @var integer
*/
public $createdBy = 0;
/**
*
* @var string
*/
public $modifiedAt = null;
/**
*
* @var integer
*/
public $modifiedBy = 0;
/**
*
* @var string
*/
public $checkedAt = null;
/**
*
* @var integer
*/
public $checkedBy = 0;
/**
*
* @var string
*/
public $metaTitle = '';
/**
*
* @var string
*/
public $metaKeys = '';
/**
*
* @var string
*/
public $metaDesc = '';
/**
*
* @var string
*/
protected $params;
/**
*
* @var integer
*/
public $ordering;
/**
*
* @var integer
*/
public $lft = 0;
/**
*
* @var integer
*/
public $rgt = 1;
/**
*
* @var integer
*/
public $level = 0;
/**
* @var integer
*/
public $hits = 0;
/**
* @var boolean
*/
public $hasRoute = false;
/**
* @var string
*/
protected $titleField = 'title';
/**
* @var boolean
*/
protected $standardMetadata = true;
/** @var array */
protected $fieldsData = [];
/** @var array */
protected $translationsFieldsData = [];
public function getParent()
{
return $this->getRelated('parent');
}
/**
* Initialize method for model.
*/
public function initialize()
{
$this->setSource('ucm_items');
}
public function getOrderFields()
{
return [
($this instanceof Nested ? 'lft' : 'ordering'),
'id',
'title',
'description',
'createdAt',
'createdBy',
];
}
public function getFilterForm()
{
return new Form('FilterForm', __DIR__ . '/Form/UcmItem/Filter.php');
}
public function getFormsManager()
{
$formsManager = new FormsManager;
$formsManager->set('general', new Form('FormData', __DIR__ . '/Form/UcmItem/General.php'));
$formsManager->set('aside', new Form('FormData', __DIR__ . '/Form/UcmItem/Aside.php'));
$formsManager->set('metadata', new Form('FormData', __DIR__ . '/Form/Metadata/Metadata.php'));
return $formsManager;
}
public function isContextPrefix($prefix)
{
return preg_match('/^' . $prefix . '/', $this->context) ? true : false;
}
public function isContextSuffix($suffix)
{
return preg_match('/-' . $suffix . '$/', $this->context) ? true : false;
}
public function prepareFormsManager(FormsManager $formsManager)
{
$asideForm = $formsManager->get('aside');
if ($this->id
&& $asideForm->has('tags')
&& ($tags = $this->getRelated('tags'))->count()
)
{
$tagIds = [];
/**
* @var Tag $tag
*/
foreach ($tags as $tag)
{
$tagIds[] = (int)$tag->id;
}
$asideForm->getField('tags')->setValue($tagIds);
}
}
public function controllerDoBeforeSave(&$validData)
{
if ($this->id == $this->parentId)
{
$this->parentId = 0;
}
return parent::beforeSave();
}
public function beforeSave()
{
if (is_array($this->image))
{
$this->image = json_encode($this->image);
}
parent::beforeSave();
}
public function controllerDoAfterSave($validData)
{
$isCategory = $this->isContextSuffix('category');
$modelsManager = $this->getModelsManager();
$dbo = Factory::getService('db');
$source = $this->getSource();
if ($this->hasRoute && empty($this->route))
{
if ($this->parentId && ($parent = self::findFirst('id = ' . (int)$this->parentId)))
{
$route = Filter::toPath($parent->route . '/' . $this->title);
}
else
{
$prefix = $isCategory ? '' : $this->context . '/';
$route = $prefix . Filter::toSlug($this->title);
}
// Don't use Phalcon Update or Save.
// Because Phalcon has conflicts with array references (hasBelongsTo, hasMany...)
$dbo->query('UPDATE `' . $source . '` SET `route` = :route WHERE `id` = :id', [
'route' => $route,
'id' => (int)$this->id,
]);
}
$modelsManager->executeQuery(
'DELETE FROM ' . UcmItemMap::class . ' WHERE itemId1 = :itemId1: AND context = :context:',
[
'itemId1' => $this->id,
'context' => 'tag',
]
);
if (!empty($validData['tags']))
{
foreach (Filter::clean($validData['tags'], 'unique') as $tagId)
{
$tagId = (int)$tagId;
if ($tagId > 0)
{
(new UcmItemMap)
->assign(
[
'itemId1' => $this->id,
'itemId2' => $tagId,
'context' => 'tag',
]
)->save();
}
}
}
$context = UcmItemHelper::prepareContext($this->context);
EventHelper::trigger('afterSaveUcm' . $context, [$this, $validData], ['Cms']);
}
public function delete() : bool
{
if ($result = parent::delete())
{
$context = UcmItemHelper::prepareContext($this->context);
$previousData = $this->toArray();
EventHelper::trigger('afterDeleteUcm' . $context, [$previousData], ['Cms']);
}
return $result;
}
public function hit($pk = null)
{
if (null === $pk)
{
$pk = $this->id;
}
$session = Factory::getService('session');
$hitsKey = $this->context . '.hits';
$hits = $session->get($hitsKey, []);
if (!isset($hits[$pk]))
{
$hits[$pk] = 1;
$session->set($hitsKey, $hits);
/** @var Mysql $db */
$db = $this->getDI()->get('db');
$db->execute('UPDATE ' . $this->getSource() . ' SET hits = hits + 1 WHERE id = :id', [
'id' => $pk,
]);
if ($pk == $this->id)
{
$this->hits++;
}
}
return $this;
}
public function t($field)
{
if (Uri::isClient('administrator'))
{
return $this->{$field};
}
static $translated = [];
$k = $this->id . $field;
if (!isset($translated[$k]))
{
$translationData = parent::getTranslations();
if (isset($translationData[$field]))
{
$value = $translationData[$field];
switch ($field)
{
case 'route':
if (empty($value))
{
$value = $this->route;
}
break;
case 'image':
if (empty($value) || in_array($value, ['[]', '{}']))
{
$value = $this->image;
}
break;
case 'params':
$tranValue = $value;
$value = new Registry($this->{$field});
$value->merge($tranValue);
break;
}
}
else
{
$value = $this->{$field};
}
$translated[$k] = $value;
}
return $translated[$k];
}
public function getLink()
{
return Uri::route($this->t('route'));
}
public function summary($fallbackDescStrLen = 100)
{
$summary = trim($this->t('summary'));
if (empty($summary))
{
$summary = StringHelper::truncate($this->t('description'), $fallbackDescStrLen);
}
return $summary;
}
public function vouchers()
{
$vouchers = [];
if ((int)$this->parentId === 117) // Merken
{
$vouchers = $this->find([
'conditions' => 'externalMerchantId = :external_merchant_id: AND sourceId = :source_id: AND externalId != :external_merchant_id:',
'bind' => [
'external_merchant_id' => $this->externalMerchantId,
'source_id' => $this->sourceId,
],
])->toArray();
// $ads = Ad::find([
// 'conditions' => 'externalMerchantId = :external_merchant_id: AND sourceId = :source_id:',
// 'bind' => [
// 'external_merchant_id' => $this->externalMerchantId,
// 'source_id' => $this->sourceId,
// ],
// ])->toArray();
// $vouchers += $ads;
}
// Nothing found, set at least one if there is a prefUrl
if (0 === count($vouchers) && (int)$this->parentId === 117 && 0 < strlen($this->prefUrl))
{
$vouchers = $this->find([
'conditions' => 'externalId = :external_id: AND sourceId = :source_id:',
'bind' => [
'external_id' => $this->externalMerchantId,
'source_id' => $this->sourceId,
],
])->toArray();
}
if (false === empty($vouchers))
{
$voucherIds = array_column($vouchers, 'id', 'id');
$vouchers = $this->find([
'conditions' => 'id IN (' . implode(',', $voucherIds) . ')',
]);
}
return $vouchers;
}
public function getFieldsData()
{
if (empty($this->fieldsData) && $this->id)
{
/** @var \Phalcon\Mvc\Model\Resultset\Simple $values */
$values = $this->getModelsManager()
->createBuilder()
->columns('name, fieldId, value')
->from(['fieldValue' => UcmFieldValue::class])
->innerJoin(UcmField::class, 'field.id = fieldValue.fieldId', 'field')
->where('field.context = :context:', ['context' => $this->context])
->andWhere('fieldValue.itemId = :thisId:', ['thisId' => $this->id])
->getQuery()
->execute();
if (0 < $values->count())
{
$fields = [];
foreach ($values as $value)
{
$this->fieldsData[$value->name] = $value->value;
$fields[$value->fieldId] = $value->name;
}
if (Language::isMultilingual())
{
$language = Language::getLanguageQuery();
$isSite = Uri::isClient('site');
if (Uri::isClient('site') && '*' === $language)
{
return $this->fieldsData;
}
// Find translation fields values
$query = $this->getModelsManager()
->createBuilder()
->from(Translation::class)
->columns('translationId, translatedValue')
->where('translationId LIKE :translationId:', [
'translationId' => ($isSite ? $language : '%') . '.ucm_field_values.fieldId=%,itemId=' . $this->id . '%',
]);
$trans = $query->getQuery()->execute();
if ($trans->count())
{
foreach ($trans as $tran)
{
$parts = explode('.', $tran->translationId);
$langCode = $parts[0];
$refKey = explode(',', $parts[2], 2);
$fieldId = str_replace('fieldId=', '', $refKey[0]);
if (isset($fields[$fieldId]))
{
$fieldName = $fields[$fieldId];
if ($isSite)
{
$this->translationsFieldsData[$fieldName] = $tran->translatedValue;
}
else
{
$this->translationsFieldsData[$fieldName][$langCode] = $tran->translatedValue;
}
}
}
}
}
}
}
return $this->fieldsData;
}
public function getTranslationsFieldsData()
{
return $this->translationsFieldsData;
}
public function getFieldValue($fieldName, $defaultValue = null)
{
$value = isset($this->translationsFieldsData[$fieldName])
? $this->translationsFieldsData[$fieldName]
: (isset($this->fieldsData[$fieldName]) ? $this->fieldsData[$fieldName] : $defaultValue);
if (strpos($value, '{') === 0 || strpos($value, '[') === 0)
{
$value = implode(', ', json_decode($value, true) ?: []);
}
return $value;
}
public function copy()
{
if ($result = parent::copy())
{
$fieldsValues = UcmFieldValue::find(
[
'conditions' => 'itemId = :itemId:',
'bind' => [
'itemId' => $this->id,
],
]
);
if ($fieldsValues->count())
{
$values = '';
$bind = [];
foreach ($fieldsValues as $fieldsValue)
{
$values .= '(?, ?, ?),';
$bind[] = $fieldsValue->fieldId;
$bind[] = $result->id;
$bind[] = $fieldsValue->value;
}
$source = $this->getModelsManager()->getModelPrefix() . 'ucm_field_values';
$this->getDI()->get('db')->execute('INSERT INTO ' . $source . ' (fieldId, itemId, value) VALUES ' . rtrim($values, ','), $bind);
}
}
return $result;
}
protected function afterDelete()
{
parent::afterDelete();
$source = $this->getModelsManager()->getModelPrefix() . 'ucm_field_values';
$this->getDI()->get('db')->execute('DELETE FROM ' . $source . ' WHERE itemId LIKE :itemId', [
'itemId' => $this->id,
]);
}
public function getParams()
{
if (!($this->params instanceof Registry))
{
$this->params = new Registry($this->params);
}
return $this->params;
}
}
|
#11 | Phalcon\Mvc\Application->handle
/var/www/html/discounts-n-coupons.com/src/app/Lib/CmsApplication.php (120) <?php
namespace CLSystems\PhalCMS\Lib;
use Phalcon\Autoload\Loader;
use Phalcon\Http\Response;
use Phalcon\Events\Event;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use CLSystems\PhalCMS\Lib\Helper\Asset;
use CLSystems\PhalCMS\Lib\Helper\Config;
use CLSystems\PhalCMS\Lib\Helper\Uri;
use CLSystems\PhalCMS\Lib\Helper\State;
use CLSystems\PhalCMS\Lib\Helper\User;
use CLSystems\PhalCMS\Lib\Helper\Event as EventHelper;
use CLSystems\PhalCMS\Lib\Mvc\View\ViewBase;
use CLSystems\Php\Registry;
use MatthiasMullie\Minify;
use Exception;
class CmsApplication extends Application
{
public function execute()
{
try {
$eventsManager = $this->di->getShared('eventsManager');
$eventsManager->attach('application:beforeSendResponse', $this);
$plugins = EventHelper::getPlugins();
$systemEvents = [
'application:beforeSendResponse',
'dispatch:beforeExecuteRoute',
'dispatch:beforeException',
'dispatch:beforeDispatch',
'dispatch:afterDispatch',
'dispatch:afterInitialize',
];
if(false === empty($plugins['System']))
{
foreach ($plugins['System'] as $className => $config) {
$handler = EventHelper::getHandler($className, $config);
foreach ($systemEvents as $systemEvent) {
$eventsManager->attach($systemEvent, $handler);
}
}
}
// Update view dirs
define('TPL_SITE_PATH', APP_PATH . '/Tmpl/Site/' . Config::get('siteTemplate', 'PhalCMS'));
define('TPL_ADMINISTRATOR_PATH', APP_PATH . '/Tmpl/Administrator');
define('TPL_SYSTEM_PATH', APP_PATH . '/Tmpl/System');
if (Uri::isClient('site')) {
$viewDirs = [
TPL_SITE_PATH . '/Tmpl/',
TPL_SITE_PATH . '/',
];
} else {
$viewDirs = [
TPL_ADMINISTRATOR_PATH . '/',
];
}
foreach (['System', 'Cms'] as $plgGroup) {
if (isset($plugins[$plgGroup])) {
/**
* @var string $pluginClass
* @var Registry $pluginConfig
*/
foreach ($plugins[$plgGroup] as $pluginClass => $pluginConfig) {
$pluginName = $pluginConfig->get('manifest.name');
$pluginPath = PLUGIN_PATH . '/' . $plgGroup . '/' . $pluginName;
$psrPaths = [];
if (is_dir($pluginPath . '/Tmpl')) {
$viewDirs[] = $pluginPath . '/Tmpl/';
}
if (is_dir($pluginPath . '/Lib')) {
$psrPaths['CLSystems\\PhalCMS\\Lib'] = $pluginPath . '/Lib';
}
if (is_dir($pluginPath . '/Widget')) {
$psrPaths['CLSystems\\PhalCMS\\Widget'] = $pluginPath . '/Widget';
}
if ($psrPaths) {
(new Loader)
->setNamespaces($psrPaths, true)
->register();
}
}
}
}
$viewDirs[] = TPL_SYSTEM_PATH . '/';
/** @var ViewBase $view */
$view = $this->di->getShared('view');
$requestUri = $_SERVER['REQUEST_URI'];
if (Config::get('siteOffline') === 'Y'
&& !User::getInstance()->access('super')
) {
$this->view->setMainView('Offline/Index');
if (!str_starts_with($requestUri, '/user/')) {
$requestUri = '';
}
} else {
$view->setMainView('Index');
}
$view->setViewsDir($viewDirs);
$this->setEventsManager($eventsManager);
$this->handle($requestUri)->send();
} catch (Exception $e) {
if (true === DEVELOPMENT_MODE) {
// Let Phalcon Debug catch this
throw $e;
}
try {
if (User::getInstance()->access('super')) {
State::setMark('exception', $e);
}
/**
* @var Dispatcher $dispatcher
* @var View $view
*/
$dispatcher = $this->getDI()->getShared('dispatcher');
$dispatcher->setControllerName(Uri::isClient('administrator') ? 'admin_error' : 'error');
$dispatcher->setActionName('show');
$dispatcher->setParams(
[
'code' => $e->getCode(),
'message' => $e->getMessage(),
]
);
$view = $this->getDI()->getShared('view');
$view->start();
$dispatcher->dispatch();
$view->render(
$dispatcher->getControllerName(),
$dispatcher->getActionName(),
$dispatcher->getParams()
);
$view->finish();
echo $view->getContent();
} catch (Exception $e2) {
debugVar($e2->getMessage());
}
}
}
protected function getCompressor($type)
{
if ('css' === $type) {
$compressor = new Minify\CSS;
$compressor->setImportExtensions(
[
'gif' => 'data:image/gif',
'jpg' => 'data:image/jpg',
'png' => 'data:image/png',
'svg' => 'data:image/svg+xml',
]
);
} else {
$compressor = new Minify\JS;
}
return $compressor;
}
protected function compressAssets()
{
$basePath = PUBLIC_PATH . '/assets';
$assets = Factory::getService('assets');
foreach (Asset::getFiles() as $type => $files) {
$fileName = md5(implode(':', $files)) . '.' . $type;
$filePath = $basePath . '/compressed/' . $fileName;
$fileUri = DOMAIN . '/assets/compressed/' . $fileName . (DEVELOPMENT_MODE ? '?' . time() : '');
$hasAsset = is_file($filePath);
$ucType = ucfirst($type);
$addFunc = 'add' . $ucType;
if ($hasAsset && !DEVELOPMENT_MODE) {
call_user_func_array([$assets, $addFunc], [$fileUri, false]);
continue;
}
$compressor = self::getCompressor($type);
foreach ($files as $file) {
$compressor->add($file);
}
if (!is_dir($basePath . '/compressed/')) {
mkdir($basePath . '/compressed/', 0777, true);
}
if ($compressor->minify($filePath)) {
chmod($filePath, 0777);
call_user_func_array([$assets, $addFunc], [$fileUri, false]);
}
unset($compressor);
}
}
public function beforeSendResponse(Event $event, CmsApplication $app, Response $response)
{
$request = $this->di->getShared('request');
if ($request->isAjax()) {
return;
}
/** @var Asset $assets */
$this->compressAssets();
$assets = $this->di->getShared('assets');
// Compress CSS
ob_start();
$assets->outputCss();
$assets->outputInlineCss();
$content = str_replace('</head>', ob_get_clean() . '</head>', $response->getContent());
// Compress JS
ob_start();
$assets->outputJs();
$assets->outputInlineJs();
$code = Asset::getCode() . ob_get_clean();
// Extra code (in the footer)
$content = str_replace('</body>', $code . '</body>', $content);
$response->setContent($content);
}
}
|