Skip to content

Commit

Permalink
file type in catalog fields
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed Oct 1, 2015
1 parent cfc6398 commit 965ecd9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
8 changes: 4 additions & 4 deletions components/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function init()

public static function __callStatic($method, $params)
{
$name = (new \ReflectionClass(self::className()))->getShortName();
if (!isset(self::$classes[$name])) {
self::$classes[$name] = new static();
$name = static::className();
if (!isset(static::$classes[$name])) {
static::$classes[$name] = new static();
}
return call_user_func_array([self::$classes[$name], 'api_' . $method], $params);
return call_user_func_array([static::$classes[$name], 'api_' . $method], $params);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions helpers/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ static function getLink($fileName)
return str_replace('\\', '/', str_replace(Yii::getAlias('@webroot'), '', $fileName));
}

static function getFileName($fileInstanse, $namePostfix = true)
static function getFileName($fileInstance, $namePostfix = true)
{
$baseName = str_ireplace('.'.$fileInstanse->extension, '', $fileInstanse->name);
$baseName = str_ireplace('.'.$fileInstance->extension, '', $fileInstance->name);
$fileName = StringHelper::truncate(Inflector::slug($baseName), 32, '');
if($namePostfix || !$fileName) {
$fileName .= ($fileName ? '-' : '') . substr(uniqid(md5(rand()), true), 0, 10);
}
$fileName .= '.' . $fileInstanse->extension;
$fileName .= '.' . $fileInstance->extension;

return $fileName;
}
Expand Down
11 changes: 6 additions & 5 deletions modules/catalog/controllers/AController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ public function actionFields($id)
){
continue;
}
$options = '';
$options = trim($temp->options);
if($temp->type == 'select' || $temp->type == 'checkbox'){
if(empty($temp->options) || !($temp->options = trim($temp->options))){
if($options == ''){
continue;
}
$options = [];
foreach(explode(',', $temp->options) as $option){
$options[] = trim($option);
$optionsArray = [];
foreach(explode(',', $options) as $option){
$optionsArray[] = trim($option);
}
$options = $optionsArray;
}

$result[] = [
Expand Down
60 changes: 58 additions & 2 deletions modules/catalog/controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Yii;
use yii\easyii\behaviors\StatusController;
use yii\easyii\helpers\Upload;
use yii\validators\FileValidator;
use yii\web\UploadedFile;
use yii\helpers\Html;

Expand All @@ -15,6 +17,8 @@

class ItemsController extends Controller
{
static $RESTRICTED_EXTENSIONS = ['php', 'phtml', 'php5', 'htm', 'html', 'js', 'jsp', 'sh', 'exe', 'bat', 'com'];

public function behaviors()
{
return [
Expand Down Expand Up @@ -56,7 +60,7 @@ public function actionCreate($id)
}
else {
$model->category_id = $category->primaryKey;
$model->data = Yii::$app->request->post('Data');
$this->parseData($model);

if (isset($_FILES) && $this->module->settings['itemThumb']) {
$model->image = UploadedFile::getInstance($model, 'image');
Expand Down Expand Up @@ -96,7 +100,7 @@ public function actionEdit($id)
return ActiveForm::validate($model);
}
else {
$model->data = Yii::$app->request->post('Data');
$this->parseData($model);

if (isset($_FILES) && $this->module->settings['itemThumb']) {
$model->image = UploadedFile::getInstance($model, 'image');
Expand Down Expand Up @@ -164,6 +168,21 @@ public function actionDelete($id)
return $this->formatResponse(Yii::t('easyii/catalog', 'Item deleted'));
}

public function actionDeleteDataFile($file)
{
foreach(Item::find()->where(['like', 'data', $file])->all() as $model) {

foreach ($model->data as $name => $value) {
if (!is_array($value) && strpos($value, '/' . $file) !== false) {
@unlink(Yii::getAlias('@webroot') . $value);
$model->data->{$name} = '';
}
}
$model->update();
}
return $this->formatResponse(Yii::t('easyii', 'Deleted'));
}

public function actionUp($id, $category_id)
{
return $this->move($id, 'up', ['category_id' => $category_id]);
Expand Down Expand Up @@ -214,7 +233,44 @@ private function generateForm($fields, $data = null)
}
$result .= '<div class="checkbox well well-sm"><b>'. $field->title .'</b>'. $options .'</div>';
}
elseif ($field->type === 'file') {
$result .= '<div class="form-group"><label>'. $field->title .'</label>'. Html::fileInput("Data[{$field->name}]");
if($value != ''){
$basename = basename($value);
$result .=
'<p>' .
Html::a($basename, [$value], ['target' => 'blank']) .
' ' .
Html::a('<i class="glyphicon glyphicon-remove"></i>', ['/admin/catalog/items/delete-data-file', 'file' => $basename], ['class' => 'confirm-delete', 'data-reload' => 1, 'title' => Yii::t('easyii', 'Delete')]);
'</p>';
}
$result .= '</div>';
}
}
return $result;
}

private function parseData(&$model)
{
$data = Yii::$app->request->post('Data');

if(isset($_FILES['Data']))
{
foreach($_FILES['Data']['name'] as $fieldName => $sourceName){
$field = $model->category->getFieldByName($fieldName);
$validator = new FileValidator(['extensions' => $field->options ? $field->options : null]);
$uploadInstance = UploadedFile::getInstanceByName('Data['.$fieldName.']');
if($uploadInstance && !in_array($uploadInstance->extension, self::$RESTRICTED_EXTENSIONS) && $validator->validate($uploadInstance) && ($result = Upload::file($uploadInstance, 'catalog/files', false))) {
if(!empty($model->data->{$fieldName})){
@unlink(Yii::getAlias('@webroot') . $model->data->{$fieldName});
}
$data[$fieldName] = $result;
} else {
$data[$fieldName] = !empty($model->data->{$fieldName}) ? $model->data->{$fieldName} : '';
}
}
}

$model->data = $data;
}
}
2 changes: 1 addition & 1 deletion modules/catalog/media/js/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ $(function(){

function optionsIsNeeded(type)
{
return type == 'select' || type == 'checkbox';
return type == 'select' || type == 'checkbox' || type == 'file';
}
});
16 changes: 14 additions & 2 deletions modules/catalog/models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Category extends \yii\easyii\components\CategoryModel
'text' => 'Text',
'boolean' => 'Boolean',
'select' => 'Select',
'checkbox' => 'Checkbox'
'checkbox' => 'Checkbox',
'file' => 'File'
];

public static function tableName()
Expand Down Expand Up @@ -60,7 +61,18 @@ public function afterDelete()
}
}

private function parseFields(){
public function getFieldByName($name)
{
foreach($this->fields as $field){
if($field->name == $name){
return $field;
}
}
return null;
}

private function parseFields()
{
$this->fields = $this->fields !== '' ? json_decode($this->fields) : [];
}
}
2 changes: 1 addition & 1 deletion modules/catalog/views/a/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</select>
</td>
<td>
<textarea class="form-control field-options" placeholder="<?= Yii::t('easyii/catalog', 'Type options with `comma` as delimiter') ?>" <?= !$field->options ? 'style="display: none;"' : '' ?> ><?= is_array($field->options) ? implode(',', $field->options) : '' ?></textarea>
<textarea class="form-control field-options" placeholder="<?= Yii::t('easyii/catalog', 'Type options with `comma` as delimiter') ?>" <?= (!$field->options && $field->type != 'file') ? 'style="display: none;"' : '' ?> ><?= is_array($field->options) ? implode(',', $field->options) : $field->options ?></textarea>
</td>
<td class="text-right">
<div class="btn-group btn-group-sm" role="group">
Expand Down

0 comments on commit 965ecd9

Please sign in to comment.