Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Lyshenko committed Sep 28, 2015
1 parent 06522e8 commit a939319
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 189 deletions.
7 changes: 4 additions & 3 deletions src/controllers/UtilController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace vladdnepr\ycm\utils\controllers;

use vladdnepr\ycm\utils\helpers\ModelHelper;
use vladdnepr\ycm\utils\models\YcmModelUtilTrait;
use vladdnepr\ycm\utils\Module;
use Yii;
Expand Down Expand Up @@ -57,11 +58,11 @@ public function actionChoices($name, $q = null, $id = null)
}

if (!is_null($q)) {
$out['results'] = $model->findChoicesByLabel($q);
$out['results'] = ModelHelper::findChoicesByLabel($model, $q);
} elseif ($id > 0) {
$out['results'] = [
$model->getPkColumnName() => $id,
'text' => $model->find($id)->getLabelColumnValue()
ModelHelper::getPkColumnName($model) => $id,
'text' => ModelHelper::getLabelColumnName($model->findOne($id))
];
}
return $out;
Expand Down
154 changes: 154 additions & 0 deletions src/helpers/ModelHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace vladdnepr\ycm\utils\helpers;

use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;

class ModelHelper
{
protected static $select_choices_cache;
protected static $label_columns_cache;

protected static $label_column_default = ['title', 'name', 'id'];

/**
* Get Choices
* @param ActiveRecord $model
* @return array Key - primary key value, value - label column value
*/
public static function getSelectChoices(ActiveRecord $model)
{
$title_column_name = self::getLabelColumnName($model);
$pk_column_name = self::getPkColumnName($model);

$key = $model->className().$title_column_name.$pk_column_name;

if (!isset(self::$select_choices_cache[$key])) {
self::$select_choices_cache[$key] = ArrayHelper::map(
$model->find()->orderBy($title_column_name . ' ASC')->all(),
$pk_column_name,
$title_column_name
);
}

return self::$select_choices_cache[$key];
}

public static function getEnumChoices(ActiveRecord $model, $attribute)
{
$values = [];

if (($columnSchema = $model->getTableSchema()->getColumn($attribute)) && $columnSchema->enumValues) {
$values = array_combine(
array_values($columnSchema->enumValues),
array_map('ucfirst', $columnSchema->enumValues)
);
}

return $values;
}

public static function getBooleanChoices(ActiveRecord $model, $attribute)
{
$values = [];

if (($columnSchema = $model->getTableSchema()->getColumn($attribute))
&& strpos($columnSchema->dbType, 'tinyint(1)') !== false
) {
$values = [
0 => 'No',
1 => 'Yes'
];
}

return $values;
}

/**
* Get label column name
* @param ActiveRecord $model
* @return mixed
*/
public static function getLabelColumnName(ActiveRecord $model)
{
$class = $model->className();

if (!isset(self::$label_columns_cache[$class])) {
$schema = $model->getTableSchema();
$available_names = array_intersect(static::$label_column_default, $schema->getColumnNames());

self::$label_columns_cache[$class] = reset($available_names);
}

return self::$label_columns_cache[$class];
}

/**
* Get label column value
* @param ActiveRecord $model
* @return array|null
*/
public static function getLabelColumnValue(ActiveRecord $model)
{
return $model->{self::getLabelColumnName($model)};
}

/**
* Get PK column name
* @param ActiveRecord $model
* @return mixed
*/
public static function getPkColumnName(ActiveRecord $model)
{
return $model->getTableSchema()->primaryKey[0];
}

/**
* Get PK column value
* @param ActiveRecord $model
* @return mixed
* @throws \yii\base\InvalidConfigException
*/
public static function getPkColumnValue(ActiveRecord $model)
{
return $model->{self::getPkColumnName($model)};
}

/**
* Find model by label value
* @param ActiveRecord $model
* @param string $label
* @return null|static
*/
public static function findByLabel(ActiveRecord $model, $label)
{
/* @var ActiveRecord|static $this */
return $model->findOne([self::getLabelColumnName($model) => $label]);
}

/**
* Find choices by label value
* @param ActiveRecord $model
* @param string $label
* @param int $limit
* @return array
*/
public static function findChoicesByLabel(ActiveRecord $model, $label, $limit = 20)
{
/* @var ActiveRecord|static $this */
$pk_column = self::getPkColumnName($model);
$label_column = self::getLabelColumnName($model);

$query = new Query();
$query->select($pk_column . ' as id, ' . $label_column .' AS text')
->from($model->tableName())
->where($label_column . ' LIKE "%' . $label .'%"')
->limit($limit);

$command = $query->createCommand();

return array_values($command->queryAll());
}
}
34 changes: 34 additions & 0 deletions src/helpers/RelationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace vladdnepr\ycm\utils\helpers;

use yii\db\ActiveRecord;

class RelationHelper
{
protected static $relationsChoices = [];
protected static $relationsIds = [];

/**
* Get available relation choices
* @param $relation_name
* @return mixed
*/
public static function getSelectChoices(ActiveRecord $model, $relation_name)
{
$class = $model->className();

if (!isset(self::$relationsChoices[$class][$relation_name])) {
self::$relationsChoices[$class][$relation_name] = [];

$relation = $model->getRelation($relation_name, false);

if ($relation) {
self::$relationsChoices[$class][$relation_name] =
ModelHelper::getSelectChoices(new $relation->modelClass);
}
}

return self::$relationsChoices[$class][$relation_name];
}
}
38 changes: 35 additions & 3 deletions src/helpers/EditHelper.php → src/helpers/admin/EditHelper.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace vladdnepr\ycm\utils\helpers;
namespace vladdnepr\ycm\utils\helpers\admin;

use kartik\select2\Select2;
use vladdnepr\ycm\utils\helpers\ModelHelper;
use vladdnepr\ycm\utils\helpers\RelationHelper;
use vladdnepr\ycm\utils\models\YcmModelUtilTrait;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
Expand All @@ -19,7 +21,7 @@ public static function relation(ActiveRecord $model, $relation_name, $options =
$relationField,
'widget',
'widgetClass' => Select2::className(),
'data' => $model->getRelationChoices($relation_name),
'data' => RelationHelper::getSelectChoices($model, $relation_name),
'hideSearch' => false,
'options' => [
'multiple' => $relation->multiple,
Expand All @@ -35,4 +37,34 @@ public static function relation(ActiveRecord $model, $relation_name, $options =
$options
);
}
}

public static function boolean(ActiveRecord $model, $attribute, $options = [])
{
$config = [$attribute, 'checkbox'];

return ArrayHelper::merge(
$config,
$options
);
}

public static function enumerate(ActiveRecord $model, $attribute, $options = [])
{
$choices = ModelHelper::getEnumChoices($model, $attribute);

$config = [
$attribute,
'widget',
'widgetClass' => Select2::className(),
'data' => $choices,
'options' => [
'placeholder' => 'Select...',
]
];

return ArrayHelper::merge(
$config,
$options
);
}
}
Loading

0 comments on commit a939319

Please sign in to comment.