diff --git a/dev/DevelopmentAdmin.php b/dev/DevelopmentAdmin.php index c3cb2819c..ab274f4a6 100644 --- a/dev/DevelopmentAdmin.php +++ b/dev/DevelopmentAdmin.php @@ -72,6 +72,10 @@ class DevelopmentAdmin extends Controller { return new ModuleManager(); } + function viewmodel() { + return new ModelViewer(); + } + function build() { $renderer = new DebugView(); $renderer->writeHeader(); diff --git a/dev/ModelViewer.php b/dev/ModelViewer.php new file mode 100644 index 000000000..83a48d4e1 --- /dev/null +++ b/dev/ModelViewer.php @@ -0,0 +1,135 @@ +push(new ModelViewer_Model($class)); + } + return $output; + } + + /** + * Model classes, grouped by Module + */ + function Modules() { + $classes = ClassInfo::subclassesFor('DataObject'); + array_shift($classes); + + $modules = array(); + foreach($classes as $class) { + $model = new ModelViewer_Model($class); + if(!isset($modules[$model->Module])) $modules[$model->Module] = new DataObjectSet(); + $modules[$model->Module]->push($model); + } + ksort($modules); + + if(isset($this->urlParams['Action']) && isset($modules[$this->urlParams['Action']])) { + $module = $this->urlParams['Action']; + $modules = array($module => $modules[$module]); + } + + $output = new DataObjectSet(); + foreach($modules as $moduleName => $models) { + $output->push(new ArrayData(array( + 'Name' => $moduleName, + 'Models' => $models, + ))); + } + + return $output; + } + +} + +/** + * Represents a single model in the model viewer + */ +class ModelViewer_Model extends ViewableData { + protected $className; + + function __construct($className) { + $this->className = $className; + } + + function getModule() { + global $_CLASS_MANIFEST; + $className = $this->className; + if(($pos = strpos($className,'_')) !== false) $className = substr($className,0,$pos); + if(isset($_CLASS_MANIFEST[$className])) { + if(preg_match('/^'.str_replace('/','\/',preg_quote(BASE_PATH)).'\/([^\/]+)\//', $_CLASS_MANIFEST[$className], $matches)) { + return $matches[1]; + } + } + } + + function getName() { + return $this->className; + } + + function getParentModel() { + $parentClass = get_parent_class($this->className); + if($parentClass != "DataObject") return $parentClass; + } + + function Fields() { + $db = singleton($this->className)->db(); + $output = new DataObjectSet(); + $output->push(new ModelViewer_Field('ID', 'PrimaryKey')); + $output->push(new ModelViewer_Field('Created', 'Datetime')); + $output->push(new ModelViewer_Field('LastEdited', 'Datetime')); + foreach($db as $k => $v) { + $output->push(new ModelViewer_Field($k, $v)); + } + return $output; + } + + function Relations() { + $output = new DataObjectSet(); + + foreach(array('has_one','has_many','many_many') as $relType) { + $items = singleton($this->className)->$relType(); + foreach($items as $k => $v) { + $output->push(new ModelViewer_Relation($k, $v, $relType)); + } + } + return $output; + } +} + +class ModelViewer_Field extends ViewableData { + public $Name, $Type; + + function __construct($name, $type) { + $this->Name = $name; + $this->Type = $type; + } +} + +class ModelViewer_Relation extends ViewableData { + public $Name, $RelationType, $RelatedClass; + + function __construct($name, $relatedClass, $relationType) { + $this->Name = $name; + $this->RelatedClass = $relatedClass; + $this->RelationType = $relationType; + } + +} + + +?> \ No newline at end of file diff --git a/templates/ModelViewer.ss b/templates/ModelViewer.ss new file mode 100644 index 000000000..652e69cc4 --- /dev/null +++ b/templates/ModelViewer.ss @@ -0,0 +1,31 @@ + + + Data Model + + + +

Data Model for your project

+ + <% control Modules %> +

Module $Name

+ + <% control Models %> +

$Name <% if ParentModel %> (subclass of $ParentModel)<% end_if %>

+

Fields

+ + +

Relations

+ + <% end_control %> + <% end_control %> + + + \ No newline at end of file