Skip to content

Commit

Permalink
Asterisk argument for model field generates crud for all application …
Browse files Browse the repository at this point in the history
…models
  • Loading branch information
motin committed Jul 29, 2013
1 parent ccee491 commit ad35929
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 60 deletions.
106 changes: 104 additions & 2 deletions fullCrud/FullCrudCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,40 @@ protected function defaultControllerId($model)

}

/**
* Note: Asterisk argument for model field generates crud for all application models
*/
public function prepare()
{

$models = array();
$this->files=array();

if (strpos($this->model, '*') === 0) {
$models = array_keys($this->getModels());
} else {
$models[] = $this->model;
}

foreach ($models as $model) {
if (strpos($model, "application.") === false) {
continue;
}
$this->model = $model;
$this->controller = $this->defaultControllerId($this->model);
$this->validateModel('model', array());
if ($this->hasErrors('model')) {
continue;
};
$this->prepareSingle();
$this->model = '*';
}

}

public function prepareSingle()
{

if (!$this->controller) {
$this->controller = $this->defaultControllerId($this->model);
}
Expand All @@ -151,7 +183,6 @@ public function prepare()
}

// Adapted code from original CrudCode->prepare() to support a more flexible template structure
$this->files=array();
$templatePath=$this->templatePath;

// Add the controller view manually
Expand Down Expand Up @@ -207,7 +238,7 @@ protected function addFilesFromPath($templatePath, $viewPathAlias)
}
}

}
}

/**
* Overridden not to supply controller id, which we do manually
Expand All @@ -220,6 +251,11 @@ public function getViewPath()

public function validateModel($attribute, $params)
{
if ($this->model === "*") {
$this->clearErrors('model');
$this->clearErrors('controller');
return true;
}
// check your import paths, if you get an error here
// PHP error can't be catched as an exception
if ($this->model) {
Expand Down Expand Up @@ -376,6 +412,72 @@ private function getOutputViewDirectory()
return $viewDir;
}

/**
* Returns the model names and, if possible, the attributes in an array.
* Only non abstract and superclasses of CActiveRecord models are returned.
* @return array key = names and value = attributes of the models
*/
public function getModels()
{
$models = array();
$aliases = array();
$aliases[] = 'application.models';
foreach (Yii::app()->getModules() as $moduleName => $config) {
if ($moduleName != 'gii')
$aliases[] = $moduleName . ".models";
}

foreach ($aliases as $alias) {
if (!is_dir(Yii::getPathOfAlias($alias)))
continue;
$files = scandir(Yii::getPathOfAlias($alias));
Yii::import($alias . ".*");
foreach ($files as $file) {
if ($fileClassName = $this->checkFile($file, $alias)) {
$classname = sprintf('%s.%s', $alias, $fileClassName);
Yii::import($classname);
try {
if (!class_exists($fileClassName))
throw new Exception('Model '.$fileClassName.' does not exist');

$model = new $fileClassName;
if (is_object($model) && $model->getMetaData())
$models[$classname] = $model->attributes;
else
$models[$classname] = array();
} catch (ErrorException $e) {
break;
} catch (CDbException $e) {
break;
} catch (Exception $e) {
break;
}
}
}
}

return $models;
}

private function checkFile($file, $alias = '')
{
if (substr($file, 0, 1) !== '.'
&& substr($file, 0, 2) !== '..'
&& substr($file, 0, 4) !== 'Base'
&& $file != 'GActiveRecord'
&& strtolower(substr($file, -4)) === '.php') {
$fileClassName = substr($file, 0, strpos($file, '.'));
if (class_exists($fileClassName)
&& is_subclass_of($fileClassName, 'CActiveRecord')) {
$fileClass = new ReflectionClass($fileClassName);
if ($fileClass->isAbstract())
return null;
else
return $models[] = $fileClassName;
}
}
}

}

?>
61 changes: 3 additions & 58 deletions fullCrud/FullCrudGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,15 @@ class FullCrudGenerator extends CCodeGenerator
public $codeModel = 'gtc.fullCrud.FullCrudCode';

/**
* Returns the model names and, if possible, the attributes in an array.
* Only non abstract and superclasses of CActiveRecord models are returned.
* The array is used to build the autocomplete field and the
* list of possible columns in the FullCrud form.
* @return array key = names and value = attributes of the models
*/
protected function getModels()
{
$models = array();
$aliases = array();
$aliases[] = 'application.models';
foreach (Yii::app()->getModules() as $moduleName => $config) {
if ($moduleName != 'gii')
$aliases[] = $moduleName . ".models";
}

foreach ($aliases as $alias) {
if (!is_dir(Yii::getPathOfAlias($alias)))
continue;
$files = scandir(Yii::getPathOfAlias($alias));
Yii::import($alias . ".*");
foreach ($files as $file) {
if ($fileClassName = $this->checkFile($file, $alias)) {
$classname = sprintf('%s.%s', $alias, $fileClassName);
Yii::import($classname);
try {
if (!class_exists($fileClassName))
throw new Exception('Model '.$fileClassName.' does not exist');

$model = new $fileClassName;
if (is_object($model) && $model->getMetaData())
$models[$classname] = $model->attributes;
else
$models[$classname] = array();
} catch (ErrorException $e) {
break;
} catch (CDbException $e) {
break;
} catch (Exception $e) {
break;
}
}
}
}

$codeModel = Yii::createComponent($this->codeModel);
$models = $codeModel->getModels();
$models[] = "*";
return $models;
}

Expand All @@ -71,25 +35,6 @@ protected function getAuthTemplates($template)
return $return;
}

private function checkFile($file, $alias = '')
{
if (substr($file, 0, 1) !== '.'
&& substr($file, 0, 2) !== '..'
&& substr($file, 0, 4) !== 'Base'
&& $file != 'GActiveRecord'
&& strtolower(substr($file, -4)) === '.php') {
$fileClassName = substr($file, 0, strpos($file, '.'));
if (class_exists($fileClassName)
&& is_subclass_of($fileClassName, 'CActiveRecord')) {
$fileClass = new ReflectionClass($fileClassName);
if ($fileClass->isAbstract())
return null;
else
return $models[] = $fileClassName;
}
}
}

}

?>

0 comments on commit ad35929

Please sign in to comment.