first
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Model;
|
||||
|
||||
|
||||
use Nextend\Framework\Pattern\MVCHelperTrait;
|
||||
|
||||
abstract class AbstractModel {
|
||||
|
||||
use MVCHelperTrait;
|
||||
|
||||
/**
|
||||
* AbstractModel constructor.
|
||||
*
|
||||
* @param MVCHelperTrait $helper
|
||||
*/
|
||||
public function __construct($helper) {
|
||||
|
||||
$this->setMVCHelper($helper);
|
||||
$this->init();
|
||||
}
|
||||
|
||||
protected function init() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Model;
|
||||
|
||||
|
||||
use Nextend\Framework\Database\AbstractPlatformConnectorTable;
|
||||
|
||||
abstract class AbstractModelTable extends AbstractModel {
|
||||
|
||||
/**
|
||||
* @var AbstractPlatformConnectorTable
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
protected function init() {
|
||||
|
||||
$this->table = $this->createConnectorTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractPlatformConnectorTable
|
||||
*/
|
||||
protected abstract function createConnectorTable();
|
||||
|
||||
public function getTableName() {
|
||||
return $this->table->getTableName();
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Model;
|
||||
|
||||
class ApplicationSection {
|
||||
|
||||
private $application = 'system';
|
||||
|
||||
/**
|
||||
* Quick cache implementation to prevent duplicate queries. It might have bugs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $cache = array();
|
||||
|
||||
public function __construct($application) {
|
||||
$this->application = $application;
|
||||
}
|
||||
|
||||
public function getById($id, $section) {
|
||||
return Section::getById($id, $section);
|
||||
}
|
||||
|
||||
public function setById($id, $value) {
|
||||
$this->cache = array();
|
||||
|
||||
return Section::setById($id, $value);
|
||||
}
|
||||
|
||||
public function get($section, $referenceKey = null, $default = null) {
|
||||
|
||||
if (isset($this->cache[$section . '///' . $referenceKey])) {
|
||||
return $this->cache[$section . '///' . $referenceKey];
|
||||
}
|
||||
|
||||
$attributes = array(
|
||||
"application" => $this->application,
|
||||
"section" => $section
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$attributes['referencekey'] = $referenceKey;
|
||||
}
|
||||
$result = Section::$tableSectionStorage->findByAttributes($attributes);
|
||||
if (is_array($result)) {
|
||||
$this->cache[$section . '///' . $referenceKey] = $result['value'];
|
||||
|
||||
return $result['value'];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getAll($section, $referenceKey = null) {
|
||||
return Section::getAll($this->application, $section, $referenceKey);
|
||||
}
|
||||
|
||||
public function set($section, $referenceKey, $value) {
|
||||
if (isset($this->cache[$section . '///' . $referenceKey])) {
|
||||
unset($this->cache[$section . '///' . $referenceKey]);
|
||||
}
|
||||
|
||||
Section::set($this->application, $section, $referenceKey, $value);
|
||||
}
|
||||
|
||||
public function add($section, $referenceKey, $value) {
|
||||
if (isset($this->cache[$section . '///' . $referenceKey])) {
|
||||
unset($this->cache[$section . '///' . $referenceKey]);
|
||||
}
|
||||
|
||||
return Section::add($this->application, $section, $referenceKey, $value);
|
||||
}
|
||||
|
||||
public function delete($section, $referenceKey = null) {
|
||||
if (isset($this->cache[$section . '///' . $referenceKey])) {
|
||||
unset($this->cache[$section . '///' . $referenceKey]);
|
||||
}
|
||||
|
||||
return Section::delete($this->application, $section, $referenceKey);
|
||||
}
|
||||
|
||||
public function deleteById($id) {
|
||||
return Section::deleteById($id);
|
||||
}
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Model;
|
||||
|
||||
use Nextend\Framework\Database\AbstractPlatformConnectorTable;
|
||||
use Nextend\Framework\Database\Database;
|
||||
use Nextend\Framework\Plugin;
|
||||
|
||||
class Section {
|
||||
|
||||
/** @var AbstractPlatformConnectorTable */
|
||||
public static $tableSectionStorage;
|
||||
|
||||
public function __construct() {
|
||||
|
||||
self::$tableSectionStorage = Database::getTable("nextend2_section_storage");
|
||||
}
|
||||
|
||||
public static function get($application, $section, $referenceKey = null) {
|
||||
$attributes = array(
|
||||
"application" => $application,
|
||||
"section" => $section
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$attributes['referencekey'] = $referenceKey;
|
||||
}
|
||||
|
||||
return self::$tableSectionStorage->findByAttributes($attributes);
|
||||
}
|
||||
|
||||
public static function getById($id, $section = null) {
|
||||
static $cache = array();
|
||||
if ($id === 0) {
|
||||
return null;
|
||||
}
|
||||
if (!isset($cache[$section])) {
|
||||
$cache[$section] = array();
|
||||
} else if (isset($cache[$section][$id])) {
|
||||
return $cache[$section][$id];
|
||||
}
|
||||
|
||||
$cache[$section][$id] = null;
|
||||
if ($section) {
|
||||
Plugin::doAction($section, array(
|
||||
$id,
|
||||
&$cache[$section][$id]
|
||||
));
|
||||
if ($cache[$section][$id]) {
|
||||
return $cache[$section][$id];
|
||||
}
|
||||
}
|
||||
|
||||
$cache[$section][$id] = self::$tableSectionStorage->findByAttributes(array(
|
||||
"id" => $id
|
||||
));
|
||||
if ($section && $cache[$section][$id]['section'] != $section) {
|
||||
$cache[$section][$id] = null;
|
||||
|
||||
return $cache[$section][$id];
|
||||
}
|
||||
|
||||
return $cache[$section][$id];
|
||||
}
|
||||
|
||||
public static function getAll($application, $section, $referenceKey = null) {
|
||||
$attributes = array(
|
||||
"application" => $application,
|
||||
"section" => $section
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$attributes['referencekey'] = $referenceKey;
|
||||
}
|
||||
|
||||
$rows = self::$tableSectionStorage->findAllByAttributes($attributes, array(
|
||||
"id",
|
||||
"referencekey",
|
||||
"value",
|
||||
"isSystem",
|
||||
"editable"
|
||||
));
|
||||
|
||||
Plugin::doAction($application . $section, array(
|
||||
$referenceKey,
|
||||
&$rows
|
||||
));
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public static function add($application, $section, $referenceKey, $value, $isSystem = 0, $editable = 1) {
|
||||
$row = array(
|
||||
"application" => $application,
|
||||
"section" => $section,
|
||||
"referencekey" => '',
|
||||
"value" => $value,
|
||||
"isSystem" => $isSystem,
|
||||
"editable" => $editable
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$row["referencekey"] = $referenceKey;
|
||||
}
|
||||
|
||||
self::$tableSectionStorage->insert($row);
|
||||
|
||||
return self::$tableSectionStorage->insertId();
|
||||
}
|
||||
|
||||
|
||||
public static function set($application, $section, $referenceKey, $value, $isSystem = 0, $editable = 1) {
|
||||
|
||||
$result = self::getAll($application, $section, $referenceKey);
|
||||
|
||||
if (empty($result)) {
|
||||
return self::add($application, $section, $referenceKey, $value, $isSystem, $editable);
|
||||
} else {
|
||||
$attributes = array(
|
||||
"application" => $application,
|
||||
"section" => $section
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$attributes['referencekey'] = $referenceKey;
|
||||
}
|
||||
self::$tableSectionStorage->update(array('value' => $value), $attributes);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static function setById($id, $value) {
|
||||
|
||||
$result = self::getById($id);
|
||||
|
||||
if ($result !== null && $result['editable']) {
|
||||
self::$tableSectionStorage->update(array('value' => $value), array(
|
||||
"id" => $id
|
||||
));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function delete($application, $section, $referenceKey = null) {
|
||||
|
||||
$attributes = array(
|
||||
"application" => $application,
|
||||
"section" => $section,
|
||||
"isSystem" => 0
|
||||
);
|
||||
|
||||
if ($referenceKey !== null) {
|
||||
$attributes['referencekey'] = $referenceKey;
|
||||
}
|
||||
|
||||
self::$tableSectionStorage->deleteByAttributes($attributes);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function deleteById($id) {
|
||||
|
||||
self::$tableSectionStorage->deleteByAttributes(array(
|
||||
"id" => $id,
|
||||
"isSystem" => 0
|
||||
));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new Section();
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Model;
|
||||
|
||||
|
||||
class StorageSectionManager {
|
||||
|
||||
/** @var ApplicationSection[] */
|
||||
private static $storageTypes = array();
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
*
|
||||
* @return ApplicationSection
|
||||
*/
|
||||
public static function getStorage($type) {
|
||||
|
||||
if (!isset(self::$storageTypes[$type])) {
|
||||
self::$storageTypes[$type] = new ApplicationSection($type);
|
||||
}
|
||||
|
||||
return self::$storageTypes[$type];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user