diff --git a/fullCrud/FullCrudCode.php b/fullCrud/FullCrudCode.php index 9004996..ef99b8b 100644 --- a/fullCrud/FullCrudCode.php +++ b/fullCrud/FullCrudCode.php @@ -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); } @@ -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 @@ -207,7 +238,7 @@ protected function addFilesFromPath($templatePath, $viewPathAlias) } } - } + } /** * Overridden not to supply controller id, which we do manually @@ -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) { @@ -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; + } + } + } + } ?> diff --git a/fullCrud/FullCrudGenerator.php b/fullCrud/FullCrudGenerator.php index 10ad3af..d97b77e 100644 --- a/fullCrud/FullCrudGenerator.php +++ b/fullCrud/FullCrudGenerator.php @@ -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; } @@ -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; - } - } - } - } ?>