Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed Apr 26, 2016
1 parent d434eaf commit 3d53bf0
Show file tree
Hide file tree
Showing 28 changed files with 166 additions and 73 deletions.
2 changes: 1 addition & 1 deletion AdminModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function init()

if (Yii::$app instanceof yii\web\Application) {
define('IS_ROOT', !Yii::$app->user->isGuest && Yii::$app->user->identity->isRoot());
define('LIVE_EDIT', !Yii::$app->user->isGuest && Yii::$app->session->get('easyii_live_edit'));
define('LIVE_EDIT_ENABLED', !Yii::$app->user->isGuest && Yii::$app->session->get('easyii_live_edit'));
}
}

Expand Down
79 changes: 55 additions & 24 deletions components/CategoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,22 @@ public static function tree()
$cache = Yii::$app->cache;
$key = static::tableName().'_tree';

$tree = $cache->get($key);
if(!$tree){
$tree = static::generateTree();
$cache->set($key, $tree, 3600);
if(empty(static::$TREE[$key])) {

$tree = $cache->get($key);
if (!$tree) {
$tree = static::generateTree();
$cache->set($key, $tree, 3600);
}
if(count($tree)) {
foreach ($tree as $cat) {
static::$TREE[$key][] = self::buildCategoryModel($cat);
}
} else {
static::$TREE[$key] = [];
}
}
return $tree;
return static::$TREE[$key];
}

/**
Expand All @@ -125,23 +135,8 @@ public static function cats()
$cache->set($key, $flat, 3600);
}
if(count($flat)) {
foreach ($flat as $id => $cat) {
$model = new static([
'id' => $id,
'parent' => $cat->parent,
'depth' => $cat->depth,
'children' => $cat->children
]);

$model->load((array)$cat, '');
if(in_array('seo', static::$RELATIONS)) {
$model->populateRelation('seo', new SeoText($cat->seo));
}
if(in_array('tags', static::$RELATIONS)) {
$model->setTagNames($cat->tags);
}
$model->afterFind();
static::$FLAT[$key][] = $model;
foreach ($flat as $cat) {
static::$FLAT[$key][] = self::buildCategoryModel($cat);
}
} else {
static::$FLAT[$key] = [];
Expand All @@ -150,6 +145,42 @@ public static function cats()
return static::$FLAT[$key];
}

private static function buildCategoryModel($data)
{
$model = new static([
'id' => $data->id,
'parent' => $data->parent,
'depth' => $data->depth,
]);

$model->load((array)$data, '');
if(in_array('seo', static::$RELATIONS)) {
$model->populateRelation('seo', new SeoText($data->seo));
}
if(in_array('tags', static::$RELATIONS)) {
$model->setTagNames($data->tags);
}
$model->afterFind();

if(!empty($data->children) && is_array($data->children)){
if(is_object($data->children[0])) {
$model->children = [];
foreach($data->children as $child) {
$model->children[] = self::buildCategoryModel($child);
}
} else {
$model->children = $data->children;
}
}

return $model;
}

/**
* @param $id_slug
* @return CategoryModel
* @throws NotFoundHttpException
*/
public static function get($id_slug)
{
foreach(static::cats() as $cat){
Expand All @@ -176,8 +207,8 @@ public static function generateTree()

foreach ($collection as $node) {
$item = $node;
unset($item['lft'], $item['rgt'], $item['order_num']);
$item['children'] = array();
$item['parent'] = '';
$item['children'] = [];

// Number of stack items
$l = count($stack);
Expand Down
3 changes: 2 additions & 1 deletion migrations/m000009_200000_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class m000009_200000_update extends \yii\db\Migration

public function up()
{
$this->addColumn(Page::tableName(), 'show_in_menu', $this->boolean()->defaultValue(0));
$this->addColumn(Page::tableName(), 'fields', $this->text());
$this->addColumn(Page::tableName(), 'data', $this->text());
$this->addColumn(Page::tableName(), 'tree', $this->integer()->defaultValue(0));
Expand All @@ -41,7 +42,7 @@ public function up()
}

MigrationHelper::appendModuleSettings('page', [
'slugImmutable' => false,
'slugImmutable' => true,
]);
MigrationHelper::appendModuleSettings('page', [
'defaultFields' => '[]',
Expand Down
8 changes: 4 additions & 4 deletions modules/article/api/ArticleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class ArticleObject extends \yii\easyii\components\ApiObject

private $_photos;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getShort(){
return LIVE_EDIT ? API::liveEdit($this->model->short, $this->editLink) : $this->model->short;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->short, $this->getEditLink()) : $this->model->short;
}

public function getText(){
return LIVE_EDIT ? API::liveEdit($this->model->text, $this->editLink, 'div') : $this->model->text;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->text, $this->getEditLink(), 'div') : $this->model->text;
}

public function getCat(){
Expand Down
4 changes: 2 additions & 2 deletions modules/article/api/CategoryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CategoryObject extends \yii\easyii\components\ApiObject
private $_adp;
private $_children;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getPages($options = []){
Expand Down
2 changes: 1 addition & 1 deletion modules/article/api/PhotoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function box($width, $height){
'rel' => 'article-'.$this->model->item_id,
'title' => $this->description
]);
return LIVE_EDIT ? API::liveEdit($a, $this->editLink) : $a;
return LIVE_EDIT_ENABLED ? API::liveEdit($a, $this->editLink) : $a;
}

public function getEditLink(){
Expand Down
4 changes: 2 additions & 2 deletions modules/carousel/api/Carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function init()
public function api_widget($width, $height, $clientOptions = [])
{
if(!count($this->_items)){
return LIVE_EDIT ? Html::a(Yii::t('easyii/carousel/api', 'Create carousel'), ['/admin/carousel/a/create'], ['target' => '_blank']) : '';
return LIVE_EDIT_ENABLED ? Html::a(Yii::t('easyii/carousel/api', 'Create carousel'), ['/admin/carousel/a/create'], ['target' => '_blank']) : '';
}
if(count($clientOptions)){
$this->clientOptions = array_merge($this->clientOptions, $clientOptions);
Expand Down Expand Up @@ -67,7 +67,7 @@ public function api_widget($width, $height, $clientOptions = [])
'items' => $items
]);

return LIVE_EDIT ? API::liveEdit($widget, Url::to(['/admin/carousel']), 'div') : $widget;
return LIVE_EDIT_ENABLED ? API::liveEdit($widget, Url::to(['/admin/carousel']), 'div') : $widget;
}

public function api_items()
Expand Down
4 changes: 2 additions & 2 deletions modules/catalog/api/CategoryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CategoryObject extends \yii\easyii\components\ApiObject
private $_adp;
private $_children;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getPages($options = []){
Expand Down
6 changes: 3 additions & 3 deletions modules/catalog/api/ItemObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class ItemObject extends \yii\easyii\components\ApiObject

private $_photos;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getDescription(){
return LIVE_EDIT ? API::liveEdit($this->model->description, $this->editLink, 'div') : $this->model->description;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->description, $this->getEditLink(), 'div') : $this->model->description;
}

public function getCat(){
Expand Down
2 changes: 1 addition & 1 deletion modules/catalog/api/PhotoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function box($width, $height){
'rel' => 'catalog-'.$this->model->item_id,
'title' => $this->description
]);
return LIVE_EDIT ? API::liveEdit($a, $this->editLink) : $a;
return LIVE_EDIT_ENABLED ? API::liveEdit($a, $this->editLink) : $a;
}

public function getEditLink(){
Expand Down
4 changes: 2 additions & 2 deletions modules/entity/api/CategoryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class CategoryObject extends \yii\easyii\components\ApiObject
private $_adp;
private $_children ;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getPages($options = []){
Expand Down
4 changes: 2 additions & 2 deletions modules/entity/api/ItemObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class ItemObject extends \yii\easyii\components\ApiObject

private $_photos;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getCat(){
Expand Down
2 changes: 1 addition & 1 deletion modules/entity/api/PhotoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function box($width, $height){
'rel' => 'entity-'.$this->model->item_id,
'title' => $this->description
]);
return LIVE_EDIT ? API::liveEdit($a, $this->editLink) : $a;
return LIVE_EDIT_ENABLED ? API::liveEdit($a, $this->editLink) : $a;
}

public function getEditLink(){
Expand Down
4 changes: 2 additions & 2 deletions modules/faq/api/FaqObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
class FaqObject extends \yii\easyii\components\ApiObject
{
public function getQuestion(){
return LIVE_EDIT ? API::liveEdit($this->model->question, $this->editLink) : $this->model->question;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->question, $this->editLink) : $this->model->question;
}

public function getAnswer(){
return LIVE_EDIT ? API::liveEdit($this->model->answer, $this->editLink) : $this->model->answer;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->answer, $this->editLink) : $this->model->answer;
}

public function getTags(){
Expand Down
4 changes: 2 additions & 2 deletions modules/file/api/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class FileObject extends \yii\easyii\components\ApiObject
public $downloads;
public $time;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getFile(){
Expand Down
4 changes: 2 additions & 2 deletions modules/gallery/api/CategoryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CategoryObject extends \yii\easyii\components\ApiObject
private $_adp;
private $_children;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getTags(){
Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/api/PhotoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function box($width, $height){
'rel' => 'album-' . ($this->rel ? $this->rel : $this->model->item_id),
'title' => $this->description
]);
return LIVE_EDIT ? API::liveEdit($a, $this->editLink) : $a;
return LIVE_EDIT_ENABLED ? API::liveEdit($a, $this->editLink) : $a;
}

public function getEditLink(){
Expand Down
12 changes: 6 additions & 6 deletions modules/guestbook/api/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class PostObject extends \yii\easyii\components\ApiObject
public $image;
public $time;

public function getName(){
return LIVE_EDIT ? API::liveEdit($this->model->name, $this->editLink) : $this->model->name;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getName(){
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->name, $this->getEditLink()) : $this->model->name;
}

public function getText(){
return LIVE_EDIT ? API::liveEdit($this->model->text, $this->editLink, 'div') : $this->model->text;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->text, $this->getEditLink(), 'div') : $this->model->text;
}

public function getAnswer(){
return LIVE_EDIT ? API::liveEdit($this->model->answer, $this->editLink, 'div') : $this->model->answer;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->answer, $this->getEditLink(), 'div') : $this->model->answer;
}

public function getDate(){
Expand Down
8 changes: 4 additions & 4 deletions modules/news/api/NewsObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class NewsObject extends \yii\easyii\components\ApiObject

private $_photos;

public function getTitle(){
return LIVE_EDIT ? API::liveEdit($this->model->title, $this->editLink) : $this->model->title;
public function getTitle($liveEditable = true){
return ($liveEditable && LIVE_EDIT_ENABLED) ? API::liveEdit($this->model->title, $this->getEditLink()) : $this->model->title;
}

public function getShort(){
return LIVE_EDIT ? API::liveEdit($this->model->short, $this->editLink) : $this->model->short;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->short, $this->getEditLink()) : $this->model->short;
}

public function getText(){
return LIVE_EDIT ? API::liveEdit($this->model->text, $this->editLink, 'div') : $this->model->text;
return LIVE_EDIT_ENABLED ? API::liveEdit($this->model->text, $this->getEditLink(), 'div') : $this->model->text;
}

public function getTags(){
Expand Down
2 changes: 1 addition & 1 deletion modules/news/api/PhotoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function box($width, $height){
'rel' => 'news-'.$this->model->item_id,
'title' => $this->description
]);
return LIVE_EDIT ? API::liveEdit($a, $this->editLink) : $a;
return LIVE_EDIT_ENABLED ? API::liveEdit($a, $this->editLink) : $a;
}

public function getEditLink(){
Expand Down
2 changes: 1 addition & 1 deletion modules/page/PageModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class PageModule extends \yii\easyii\components\Module
{
public $settings = [
'slugImmutable' => false,
'slugImmutable' => true,
'defaultFields' => '[]'
];

Expand Down
Loading

0 comments on commit 3d53bf0

Please sign in to comment.