Skip to content

Commit

Permalink
0.9a rc
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 16, 2015
1 parent 49b09d1 commit cc76819
Show file tree
Hide file tree
Showing 104 changed files with 1,215 additions and 552 deletions.
6 changes: 3 additions & 3 deletions components/ApiObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function getId(){
public function thumb($width = null, $height = null, $crop = true)
{
if($this->image && ($width || $height)){
return Image::thumb(Yii::getAlias('@webroot') . $this->image, $width, $height, $crop);
return Image::thumb($this->image, $width, $height, $crop);
}
return '';
}

public function seo($attribute){
return !empty($this->model->seo->{$attribute}) ? $this->model->seo->{$attribute} : '';
public function seo($attribute, $default = ''){
return !empty($this->model->seo->{$attribute}) ? $this->model->seo->{$attribute} : $default;
}
}
22 changes: 12 additions & 10 deletions components/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class CategoryController extends Controller
{
public $categoryClass;
public $moduleName;
public $viewRoute = '/items';

public function actionIndex()
{
$class = $this->categoryClass;
return $this->render('@easyii/views/category/index', [
'tree' => $class::tree()
'cats' => $class::cats()
]);
}

Expand All @@ -37,7 +38,7 @@ public function actionCreate($parent = null)
$model->image = UploadedFile::getInstance($model, 'image');
if($model->image && $model->validate(['image'])){
$model->image = Image::upload($model->image, $this->moduleName);
}else{
} else {
$model->image = '';
}
}
Expand All @@ -46,17 +47,16 @@ public function actionCreate($parent = null)

$parent = Yii::$app->request->post('parent', null);
if($parent && ($parentCategory = $class::findOne((int)$parent))){
$model->appendTo($parentCategory);
$model->order_num = $parentCategory->order_num;
$model->appendTo($parentCategory);
} else {
$model->attachBehavior(SortableModel::className());
$model->makeRoot();
}

if($model->save()){

if(!$model->hasErrors()){
$this->flash('success', Yii::t('easyii', 'Category created'));
return $this->redirect(['/admin/'.$this->moduleName.'/items/index', 'id' => $model->primaryKey]);
return $this->redirect(['/admin/'.$this->moduleName, 'id' => $model->primaryKey]);
}
else{
$this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
Expand Down Expand Up @@ -134,7 +134,7 @@ public function actionDelete($id)
$class = $this->categoryClass;
if(($model = $class::findOne($id))){
$model->deleteWithChildren();
} else{
} else {
$this->error = Yii::t('easyii', 'Not found');
}
return $this->formatResponse(Yii::t('easyii', 'Category deleted'));
Expand Down Expand Up @@ -169,15 +169,17 @@ private function move($id, $direction)
if(($model = $modelClass::findOne($id)))
{
$up = $direction == 'up';
$orderDir = $up ? 'ASC' : 'DESC';
$orderDir = $up ? SORT_ASC : SORT_DESC;

if($model->depth == 0){

if($model->primaryKey == $model->tree){
$swapCat = $modelClass::find()->where([$up ? '>' : '<', 'order_num', $model->order_num])->orderBy('order_num '.$orderDir)->one();
$swapCat = $modelClass::find()->where([$up ? '>' : '<', 'order_num', $model->order_num])->orderBy(['order_num' => $orderDir])->one();
if($swapCat)
{
$modelClass::updateAll(['order_num' => '-1'], ['order_num' => $swapCat->order_num]);
$modelClass::updateAll(['order_num' => $swapCat->order_num], ['order_num' => $model->order_num]);
$modelClass::updateAll(['order_num' => $model->order_num], ['order_num' => '-1']);
$model->trigger(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE);
}
} else {
$where = [
Expand Down
50 changes: 26 additions & 24 deletions components/CategoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use yii\easyii\behaviors\SeoBehavior;
use yii\easyii\helpers\Data;
use creocoder\nestedsets\NestedSetsBehavior;
use yii\easyii\helpers\Image;

class CategoryModel extends \yii\easyii\components\ActiveRecord
{
Expand Down Expand Up @@ -57,7 +58,7 @@ public function behaviors()
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if(!$this->isNewRecord && $this->image != $this->oldAttributes['image']){
if(!$insert && $this->image != $this->oldAttributes['image'] && $this->oldAttributes['image']){
@unlink(Yii::getAlias('@webroot').$this->oldAttributes['image']);
}
return true;
Expand Down Expand Up @@ -87,7 +88,7 @@ public static function tree()
});
}

public static function flat()
public static function cats()
{
return Data::cache(static::tableName().'_flat', 3600, function(){
return self::generateFlat();
Expand All @@ -113,7 +114,7 @@ public static function generateTree()
$l = count($stack);

// Check if we're dealing with different levels
while($l > 0 && $stack[$l - 1]['depth'] >= $item['depth']) {
while($l > 0 && $stack[$l - 1]->depth >= $item['depth']) {
array_pop($stack);
$l--;
}
Expand All @@ -122,15 +123,15 @@ public static function generateTree()
if ($l == 0) {
// Assigning the root node
$i = count($trees);
$trees[$i] = $item;
$trees[$i] = (object)$item;
$stack[] = & $trees[$i];

} else {
// Add node to parent
$item['parent'] = $stack[$l - 1]['category_id'];
$i = count($stack[$l - 1]['children']);
$stack[$l - 1]['children'][$i] = $item;
$stack[] = & $stack[$l - 1]['children'][$i];
$item['parent'] = $stack[$l - 1]->category_id;
$i = count($stack[$l - 1]->children);
$stack[$l - 1]->children[$i] = (object)$item;
$stack[] = & $stack[$l - 1]->children[$i];
}
}
}
Expand All @@ -147,38 +148,39 @@ public static function generateFlat()
$depth = 0;
$lastId = 0;
foreach ($collection as $node) {
$id = $node['category_id'];
$node['parent'] = '';

if($node['depth'] > $depth){
$node['parent'] = $flat[$lastId]['category_id'];
$depth = $node['depth'];
} elseif($node['depth'] == 0){
$node = (object)$node;
$id = $node->category_id;
$node->parent = '';

if($node->depth > $depth){
$node->parent = $flat[$lastId]->category_id;
$depth = $node->depth;
} elseif($node->depth == 0){
$depth = 0;
} else {
if ($node['depth'] == $depth) {
$node['parent'] = $flat[$lastId]['parent'];
if ($node->depth == $depth) {
$node->parent = $flat[$lastId]->parent;
} else {
foreach($flat as $temp){
if($temp['depth'] == $node['depth']){
$node['parent'] = $temp['parent'];
$depth = $temp['depth'];
if($temp->depth == $node->depth){
$node->parent = $temp->parent;
$depth = $temp->depth;
break;
}
}
}
}
$lastId = $id;
unset($node['lft'], $node['rgt']);
unset($node->lft, $node->rgt);
$flat[$id] = $node;
}
}

foreach($flat as &$node){
$node['children'] = [];
$node->children = [];
foreach($flat as $temp){
if($temp['parent'] == $node['category_id']){
$node['children'][] = $temp['category_id'];
if($temp->parent == $node->category_id){
$node->children[] = $temp->category_id;
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
namespace yii\easyii\components;

use Yii;
use \yii\easyii\models;
use yii\easyii\models;
use yii\helpers\Url;

class Controller extends \yii\web\Controller
{
Expand Down Expand Up @@ -45,14 +46,12 @@ public function back()

public function setReturnUrl($url = null)
{
Yii::$app->getSession()->set($this->module->id.'_return', $url ? $url : Yii::$app->request->url);
Yii::$app->getSession()->set($this->module->id.'_return', $url ? Url::to($url) : Url::current());
}

public function getReturnUrl($defaultUrl = null)
{
$url = Yii::$app->getSession()->get($this->module->id.'_return', $defaultUrl);

return $url === null ? Yii::$app->getHomeUrl() : $url;
return Yii::$app->getSession()->get($this->module->id.'_return', $defaultUrl ? Url::to($defaultUrl) : Url::to('/admin/'.$this->module->id));
}

public function formatResponse($success = '', $back = true)
Expand All @@ -61,12 +60,12 @@ public function formatResponse($success = '', $back = true)
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if($this->error){
return ['result' => 'error', 'error' => $this->error];
} else{
} else {
$response = ['result' => 'success'];
if($success) {
if(is_array($success)){
$response = array_merge(['result' => 'success'], $success);
} else{
} else {
$response = array_merge(['result' => 'success'], ['message' => $success]);
}
}
Expand All @@ -76,7 +75,7 @@ public function formatResponse($success = '', $back = true)
else{
if($this->error){
$this->flash('error', $this->error);
} else{
} else {
if(is_array($success) && isset($success['message'])){
$this->flash('success', $success['message']);
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/AdminsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function actionDelete($id)
{
if(($model = Admin::findOne($id))){
$model->delete();
} else{
} else {
$this->error = Yii::t('easyii', 'Not found');
}
return $this->formatResponse(Yii::t('easyii', 'Admin deleted'));
Expand Down
5 changes: 5 additions & 0 deletions helpers/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public static function cache($key, $duration, $callable)
}
return $data;
}

public static function getLocale()
{
return strtolower(substr(Yii::$app->language, 0, 2));
}
}
19 changes: 3 additions & 16 deletions helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidt
return Upload::getLink($fileName);
}

static function createThumbnail($fileName, $width, $height = null, $crop = true)
static function thumb($filename, $width = null, $height = null, $crop = true)
{
$fileName = str_replace(Url::base(true), '', $fileName);

$webRoot = Yii::getAlias('@webroot');
if(!strstr($fileName, $webRoot)){
$fileName = $webRoot . $fileName;
}
$thumbFolder = dirname($fileName) . DIRECTORY_SEPARATOR . ($width.($height ? 'x'.$height : ''));
$thumbFile = $thumbFolder . DIRECTORY_SEPARATOR . basename($fileName);

if(!FileHelper::createDirectory($thumbFolder)){
throw new HttpException(500, 'Cannot create "'.$thumbFolder.'". Please check write permissions.');
if(!strstr($filename, $webRoot)){
$filename = $webRoot . $filename;
}
return self::copyResizedImage($fileName, $thumbFile, $width, $height, $crop) ? Upload::getLink($thumbFile) : false;
}

static function thumb($filename, $width = null, $height = null, $crop = true)
{
if(file_exists($filename))
{
$info = pathinfo($filename);
Expand Down Expand Up @@ -97,6 +85,5 @@ static function copyResizedImage($inputFile, $outputFile, $width, $height = null
else {
throw new HttpException(500, 'Please install GD or Imagick extension');
}
return false;
}
}
29 changes: 29 additions & 0 deletions helpers/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace yii\easyii\helpers;

use Yii;
use yii\easyii\models\Setting;

class Mail
{
public static function send($toEmail, $subject, $template, $data = [], $options = [])
{
if(!filter_var($toEmail, FILTER_VALIDATE_EMAIL) || !$subject || !$template){
return false;
}

$message = Yii::$app->mailer->compose($template, $data)
->setTo(Setting::get('admin_email'))
->setSubject(trim($subject));

if(!filter_var(Setting::get('robot_email'), FILTER_VALIDATE_EMAIL)){
$message->setFrom(Setting::get('robot_email'));
}

if(!empty($options['replyTo']) && filter_var($options['replyTo'], FILTER_VALIDATE_EMAIL)){
$message->setReplyTo($options['replyTo']);
}

return $message->send();
}
}
2 changes: 1 addition & 1 deletion media/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ html{
#admin-body .main .container-fluid{
padding: 20px;
}
#admin-body .main a, #admin-body .main .btn, #admin-body .main .form-control{
#admin-body .main a, #admin-body .main .btn, #admin-body .main .form-control, .input-group-addon{
border-radius: 0 !important;
}

Expand Down
1 change: 1 addition & 0 deletions messages/ru/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
'Not found' => 'Не найдено',
'Create error. {0}' => 'Ошибка при создании. {0}',
'Update error. {0}' => 'Ошибка при обновлении. {0}',
'An error has occurred' => 'Произошла ошибка',
'Slug can contain only 0-9, a-z and "-" characters (max: 128).' => 'Метка может содержать только 0-9, a-z и "-" (макс. 128).',

'Only for developer' => 'Только для разработчика',
Expand Down
Loading

0 comments on commit cc76819

Please sign in to comment.