Skip to content

Instantly share code, notes, and snippets.

@msenkpiel
Last active August 29, 2015 14:04
Show Gist options
  • Save msenkpiel/ec461eec45230d9ec064 to your computer and use it in GitHub Desktop.
Save msenkpiel/ec461eec45230d9ec064 to your computer and use it in GitHub Desktop.
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"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment