Pimcore backend helper to add website properties via code (since 2.0.0)
<?php
/**
* Class WebsiteSettings
*/
class WebsiteSettings extends Zend_Db_Table_Abstract
{
protected $_name = 'website_settings';
protected $_primary = 'id';
}
/**
* Class Website_Tool_Backend
*/
class Website_Tool_Backend
{
/**
* @param string $name
* @param mixed $data
* @param string $type
* @param int $siteId
*
* @throws Exception
*/
public static function setWebsiteConfigKey($name, $data, $type = 'text', $siteId = 0)
{
$allowedTypes = array('text', 'document', 'asset', 'object', 'bool');
$t = time();
// defaults
$insertName = (string)$name;
$insertData = '';
$insertType = $type;
$creationDate = $t;
$modificationDate = $t;
if (!in_array($type, $allowedTypes)) {
throw new Exception('Invalid Type');
}
if ($type == 'text') {
$insertData = (string)$data;
}
if ($type == 'document') {
if ($data instanceof Document) {
$insertData = (string)$data->getId();
} else {
throw new Exception('Invalid Document');
}
}
if ($type == 'asset') {
if ($data instanceof Asset) {
$insertData = (string)$data->getId();
} else {
throw new Exception('Invalid Asset');
}
}
if ($type == 'object') {
if ($data instanceof Object_Class) {
$insertData = (string)$data->getId();
} else {
throw new Exception('Invalid Object');
}
}
if ($type == 'bool') {
$insertData = ($data) ? '1' : '';
}
/** @var WebsiteSettings $table */
$table = new WebsiteSettings();
// prepare data for insert
$dbData = array(
'name' => $insertName,
'type' => $insertType,
'data' => $insertData,
'siteId' => (int)$siteId,
'creationDate' => $creationDate,
'modificationDate' => $modificationDate
);
// delete row if exists
/** @var Zend_Db_Table_Row_Abstract $row */
$row = $table->fetchRow(
$table->select()->where('name = :name')->bind(array(':name' => $name))->order('name ASC')
);
if ($row) {
$row->delete();
}
$result = $table->insert($dbData);
if ($result) {
Pimcore_Model_Cache::clearTags(array("output", "system", "website_config"));
}
}
}