From 37a67ea723bb6d93421591b8f3ed15738f53ed9e Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sat, 12 Mar 2016 00:20:18 +0100 Subject: First import. --- .../src/Template/Bake/Controller/controller.ctp | 78 +++++++ .../Template/Bake/Element/Controller/_empty.ctp | 45 ++++ .../src/Template/Bake/Element/Controller/add.ctp | 71 ++++++ .../src/Template/Bake/Element/Controller/edit.ctp | 74 ++++++ .../src/Template/Bake/Element/Controller/index.ctp | 69 ++++++ .../CustomTheme/src/Template/Bake/Element/form.ctp | 126 ++++++++++ .../CustomTheme/src/Template/Bake/Model/entity.ctp | 127 ++++++++++ .../CustomTheme/src/Template/Bake/Model/table.ctp | 258 +++++++++++++++++++++ .../src/Template/Bake/Template/index.ctp | 179 ++++++++++++++ .../src/Template/Bake/Template/view.ctp | 215 +++++++++++++++++ .../src/View/Helper/BakeExtraHelper.php | 65 ++++++ 11 files changed, 1307 insertions(+) create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Controller/controller.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/add.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/edit.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/form.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/entity.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/table.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/index.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/view.ctp create mode 100644 generator/before-bake/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php (limited to 'generator/before-bake/plugins/CustomTheme/src') diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Controller/controller.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Controller/controller.ctp new file mode 100644 index 0000000..76407ce --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Controller/controller.ctp @@ -0,0 +1,78 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * Controller bake template file + * + * Allows templating of Controllers generated from bake. + * + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +use Cake\Utility\Inflector; + +$defaultModel = $name; +%> +\Controller<%= $prefix %>; + +use <%= $namespace %>\Controller\AppController; + +/** + * <%= $name %> Controller + * + * @property \<%= $namespace %>\Model\Table\<%= $defaultModel %>Table $<%= $defaultModel %> +<% +foreach ($components as $component): + $classInfo = $this->Bake->classInfo($component, 'Controller/Component', 'Component'); +%> + * @property <%= $classInfo['fqn'] %> $<%= $classInfo['name'] %> +<% endforeach; %> + */ +class <%= $name %>Controller extends AppController +{ +<% if ($this->BakeExtra->hasFilters($currentModelName) ): %> + + public function initialize() + { + parent::initialize(); + if ($this->request->action === 'index') { + $this->loadComponent('Paginator', [ 'limit' => 15, 'order' => [ '<%=$currentModelName%>.id' => 'DESC' ] ]); + $this->loadComponent('Search.Prg'); + } + } +<% endif; %> +<% +echo $this->Bake->arrayProperty('helpers', $helpers, ['indent' => false]); +echo $this->Bake->arrayProperty('components', $components, ['indent' => false]); +foreach($actions as $action) { + echo $this->element('Controller/' . $action); +} +%> +} diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.ctp new file mode 100644 index 0000000..02d5dfb --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.ctp @@ -0,0 +1,45 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +%> + + /** + * _empty method + * Generated when no other actions requested + * Prevents bake all to make all $scaffoldActions when unwanted + * + * @return void + */ + public function _empty() + { + } diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/add.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/add.ctp new file mode 100644 index 0000000..be3860b --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/add.ctp @@ -0,0 +1,71 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +$compact = ["'" . $singularName . "'"]; +%> + + /** + * Add method + * + * @return void Redirects on successful add, renders view otherwise. + */ + public function add() + { + $<%= $singularName %> = $this-><%= $currentModelName %>->newEntity(); + if ($this->request->is('post')) { + $<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data); + if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) { + $this->Flash->success(__('The <%= strtolower($singularHumanName) %> has been saved.')); + return $this->redirect(['action' => 'index']); + } else { + $this->Flash->error(__('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.')); + } + } +<% + $associations = array_merge( + $this->Bake->aliasExtractor($modelObj, 'BelongsTo'), + $this->Bake->aliasExtractor($modelObj, 'BelongsToMany') + ); + foreach ($associations as $assoc): + $association = $modelObj->association($assoc); + $otherName = $association->target()->alias(); + $otherPlural = $this->_variableName($otherName); +%> + $<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list'); +<% + $compact[] = "'$otherPlural'"; + endforeach; +%> + $this->set(compact(<%= join(', ', $compact) %>)); + $this->set('_serialize', ['<%=$singularName%>']); + } diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/edit.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/edit.ctp new file mode 100644 index 0000000..b1b62f2 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/edit.ctp @@ -0,0 +1,74 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +$belongsTo = $this->Bake->aliasExtractor($modelObj, 'BelongsTo'); +$belongsToMany = $this->Bake->aliasExtractor($modelObj, 'BelongsToMany'); +$compact = ["'" . $singularName . "'"]; +%> + + /** + * Edit method + * + * @param string|null $id <%= $singularHumanName %> id. + * @return void Redirects on successful edit, renders view otherwise. + * @throws \Cake\Network\Exception\NotFoundException When record not found. + */ + public function edit($id = null) + { + $<%= $singularName %> = $this-><%= $currentModelName %>->get($id, [ + 'contain' => [<%= $this->Bake->stringifyList($belongsToMany, ['indent' => false]) %>] + ]); + if ($this->request->is(['patch', 'post', 'put'])) { + $<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data); + if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) { + $this->Flash->success(__('The <%= strtolower($singularHumanName) %> has been saved.')); + return $this->redirect(['action' => 'index']); + } else { + $this->Flash->error(__('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.')); + } + } +<% + foreach (array_merge($belongsTo, $belongsToMany) as $assoc): + $association = $modelObj->association($assoc); + $otherName = $association->target()->alias(); + $otherPlural = $this->_variableName($otherName); +%> + $<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list'); +<% + $compact[] = "'$otherPlural'"; + endforeach; +%> + $this->set(compact(<%= join(', ', $compact) %>)); + $this->set('_serialize', ['<%=$singularName%>']); + } diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.ctp new file mode 100644 index 0000000..1fa2e18 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.ctp @@ -0,0 +1,69 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +use Cake\Utility\Hash; +use Cake\Utility\Inflector; +%> + + /** + * Index method + * + * @return void + */ + public function index() + { +<% $belongsTo = $this->Bake->aliasExtractor($modelObj, 'BelongsTo'); %> +<% if ($belongsTo): %> + $this->paginate = [ + 'contain' => [<%= $this->Bake->stringifyList($belongsTo, ['indent' => false]) %>] + ]; +<% endif; %> +<% if (! $this->BakeExtra->hasFilters($currentModelName)): %> + $this->set('<%= $pluralName %>', $this->paginate($this-><%= $currentModelName %>)); +<% else: %> + $query = $this-><%= $currentModelName %> + ->find('search', $this-><%= $currentModelName %>->filterParams($this->request->query)); + $this->set('<%= $pluralName %>', $this->paginate($query)); + $this->set('_serialize', ['<%= $pluralName %>']); + +<% + $extraModels = Hash::extract($this->BakeExtra->getFilters($currentModelName), "{s}.model"); + foreach ( $extraModels as $m ): + $v = Inflector::variable($m); +%> + $this->loadModel('<%= $m %>'); + $this->set('<%= $v %>', $this-><%= $m %>->find('list')->toArray()); +<% endforeach; + endif; %> + } diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/form.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/form.ctp new file mode 100644 index 0000000..2273d86 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Element/form.ctp @@ -0,0 +1,126 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +use Cake\Utility\Inflector; + +$fields = collection($fields) + ->filter(function($field) use ($schema) { + return $schema->columnType($field) !== 'binary'; + }); +%> + +
+ Form->create($<%= $singularVar %>) ?> +
+ <%= $singularHumanName %>') ?> + column($field); + if (!empty($fieldData['null'])) { +%> + echo $this->Form->input('<%= $field %>', ['options' => $<%= $keyFields[$field] %>, 'empty' => true]); +<% + } else { +%> + echo $this->Form->input('<%= $field %>', ['options' => $<%= $keyFields[$field] %>]); +<% + } + continue; + } + if (!in_array($field, ['created', 'modified', 'updated'])) { + $fieldData = $schema->column($field); + if (($fieldData['type'] === 'date') && (!empty($fieldData['null']))) { +%> + echo $this->Form->input('<%= $field %>', ['empty' => true, 'default' => '']); +<% + } else { +%> + echo $this->Form->input('<%= $field %>'); +<% + } + } + } + if (!empty($associations['BelongsToMany'])) { + foreach ($associations['BelongsToMany'] as $assocName => $assocData) { +%> + echo $this->Form->input('<%= $assocData['property'] %>._ids', ['options' => $<%= $assocData['variable'] %>]); +<% + } + } +%> + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/entity.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/entity.ctp new file mode 100644 index 0000000..5d1cb28 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/entity.ctp @@ -0,0 +1,127 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +use Cake\Utility\Inflector; +$title = $this->BakeExtra->getTitleOpts(Inflector::camelize($table)); + +$propertyHintMap = null; +if (!empty($propertySchema)) { + $propertyHintMap = $this->DocBlock->buildEntityPropertyHintTypeMap($propertySchema); +} + +$accessible = []; +if (!isset($fields) || $fields !== false) { + if (!empty($fields)) { + foreach ($fields as $field) { + $accessible[$field] = 'true'; + } + } elseif (!empty($primaryKey)) { + $accessible['*'] = 'true'; + foreach ($primaryKey as $field) { + $accessible[$field] = 'false'; + } + } +} +%> +\Model\Entity; + +use Cake\ORM\Entity; + +/** + * <%= $name %> Entity. +<% if ($propertyHintMap): %> + * +<% foreach ($propertyHintMap as $property => $type): %> +<% if ($type): %> + * @property <%= $type %> $<%= $property %> +<% else: %> + * @property $<%= $property %> +<% endif; %> +<% endforeach; %> +<% endif; %> + */ +class <%= $name %> extends Entity +{ +<% if (!empty($accessible)): %> + + /** + * Fields that can be mass assigned using newEntity() or patchEntity(). + * + * Note that when '*' is set to true, this allows all unspecified fields to + * be mass assigned. For security purposes, it is advised to set '*' to false + * (or remove it), and explicitly make individual fields accessible as needed. + * + * @var array + */ + protected $_accessible = [ +<% foreach ($accessible as $field => $value): %> + '<%= $field %>' => <%= $value %>, +<% endforeach; %> + ]; +<% endif %> +<% if (!empty($hidden)): %> + + /** + * Fields that are excluded from JSON an array versions of the entity. + * + * @var array + */ + protected $_hidden = [<%= $this->Bake->stringifyList($hidden) %>]; +<% endif %> +<% if (!empty($title)): %> + + /** + * Virtual field for pretty print in related table's views + * + * @return String + */ + protected function _getTitle() + { +<% if (array_key_exists('custom_code', $title)) { + echo "return " . $title['custom_code'] . ";\n"; + } else { + echo "return implode('" . $title['glue'] . "', array_filter([\n"; + if (array_key_exists('prefix', $title)) { + echo "\t\t\t'" . $title['prefix'] . "',\n"; + } + foreach ($title['pieces'] as $piece) { + echo "\t\t\t\$this->_properties['" . $piece . "'],\n"; + } + echo "], 'strlen'));\n"; + } +%> + } +<% endif; %> +} diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/table.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/table.ctp new file mode 100644 index 0000000..5d21755 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Model/table.ctp @@ -0,0 +1,258 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +use Cake\Utility\Inflector; +%> +\Model\Table; + +<% +$title = $this->BakeExtra->getTitleOpts($name); + +$uses = [ + "use $namespace\\Model\\Entity\\$entity;", + 'use Cake\ORM\Query;', + 'use Cake\ORM\RulesChecker;', + 'use Cake\ORM\Table;', + 'use Cake\Validation\Validator;' +]; +if ( $this->BakeExtra->hasFilters($name) ) { + $uses[] = 'use Search\Manager;'; + $behaviors['Search.Search'] = []; +} +sort($uses); +echo implode("\n", $uses); +%> + + +/** + * <%= $name %> Model + * +<% if ($associations): %> + * +<% foreach ($associations as $type => $assocs): %> +<% foreach ($assocs as $assoc): %> + * @property \Cake\ORM\Association\<%= Inflector::camelize($type) %> $<%= $assoc['alias'] %> +<% endforeach %> +<% endforeach; %> +<% endif; %> + */ +class <%= $name %>Table extends Table +{ + + /** + * Initialize method + * + * @param array $config The configuration for the Table. + * @return void + */ + public function initialize(array $config) + { + parent::initialize($config); + +<% if (!empty($table)): %> + $this->table('<%= $table %>'); +<% endif %> +<% + if (!empty($displayField) || !empty($title)): + if (!empty($title)): +%> + $this->displayField('title'); +<% else: %> + $this->displayField('<%= $displayField %>'); +<% endif; %> +<% endif; %> +<% if (!empty($primaryKey)): %> +<% if (count($primaryKey) > 1): %> + $this->primaryKey([<%= $this->Bake->stringifyList((array)$primaryKey, ['indent' => false]) %>]); +<% else: %> + $this->primaryKey('<%= current((array)$primaryKey) %>'); +<% endif %> +<% endif %> +<% if (!empty($behaviors)): %> + +<% endif; %> +<% foreach ($behaviors as $behavior => $behaviorData): %> + $this->addBehavior('<%= $behavior %>'<%= $behaviorData ? ", [" . implode(', ', $behaviorData) . ']' : '' %>); +<% endforeach %> +<% if (!empty($associations)): %> + +<% endif; %> +<% foreach ($associations as $type => $assocs): %> +<% foreach ($assocs as $assoc): + $alias = $assoc['alias']; + unset($assoc['alias']); +%> + $this-><%= $type %>('<%= $alias %>', [<%= $this->Bake->stringifyList($assoc, ['indent' => 3]) %>]); +<% endforeach %> +<% endforeach %> + } +<% if ( $this->BakeExtra->hasFilters($name) ): + $searchMethods = []; + foreach ( $this->BakeExtra->getFilters($name) as $k => $details ): + if ( $details['mode']==='value' ): + $searchMethods[] = "->" . $details['mode'] . "('$k', [ 'field' => \$this->aliasField('$k')])"; + else: + $fields = []; + foreach ( $details['columns'] as $col ): + $fields[] = "\$this->aliasField('$col')"; + endforeach; + $fields = implode(', ', $fields); + $searchMethods[] = "->" . $details['mode'] . "('$k', [\n" + . "\t\t\t\t'before' => " . $details['before'] . ",\n" + . "\t\t\t\t'after' => " . $details['after'] . ",\n" + . "\t\t\t\t'field' => [$fields]\n" + . "\t\t\t])"; + endif; + endforeach; +%> + /** + * Search plugin queries configuration + */ + public function searchConfiguration() + { + $search = new Manager($this); +<% if (!empty($searchMethods)): + $lastIndex = count($searchMethods) - 1; + $searchMethods[$lastIndex] .= ';'; +%> + $search +<% foreach ($searchMethods as $searchMethod): %> + <%= $searchMethod %> +<% endforeach; %> +<% endif; %> + return $search; + } +<% endif; %> +<% if (!empty($validation)): %> + + /** + * Default validation rules. + * + * @param \Cake\Validation\Validator $validator Validator instance. + * @return \Cake\Validation\Validator + */ + public function validationDefault(Validator $validator) + { +<% +foreach ($validation as $field => $rules): + $validationMethods = []; + foreach ($rules as $ruleName => $rule): + if ($rule['rule'] && !isset($rule['provider'])): + $validationMethods[] = sprintf( + "->add('%s', '%s', ['rule' => '%s'])", + $field, + $ruleName, + $rule['rule'] + ); + elseif ($rule['rule'] && isset($rule['provider'])): + $validationMethods[] = sprintf( + "->add('%s', '%s', ['rule' => '%s', 'provider' => '%s'])", + $field, + $ruleName, + $rule['rule'], + $rule['provider'] + ); + endif; + + if (isset($rule['allowEmpty'])): + if (is_string($rule['allowEmpty'])): + $validationMethods[] = sprintf( + "->allowEmpty('%s', '%s')", + $field, + $rule['allowEmpty'] + ); + elseif ($rule['allowEmpty']): + $validationMethods[] = sprintf( + "->allowEmpty('%s')", + $field + ); + else: + $validationMethods[] = sprintf( + "->requirePresence('%s', 'create')", + $field + ); + $validationMethods[] = sprintf( + "->notEmpty('%s')", + $field + ); + endif; + endif; + endforeach; + + if (!empty($validationMethods)): + $lastIndex = count($validationMethods) - 1; + $validationMethods[$lastIndex] .= ';'; + %> + $validator + <%- foreach ($validationMethods as $validationMethod): %> + <%= $validationMethod %> + <%- endforeach; %> + +<% + endif; +endforeach; +%> + return $validator; + } +<% endif %> +<% if (!empty($rulesChecker)): %> + + /** + * Returns a rules checker object that will be used for validating + * application integrity. + * + * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. + * @return \Cake\ORM\RulesChecker + */ + public function buildRules(RulesChecker $rules) + { + <%- foreach ($rulesChecker as $field => $rule): %> + $rules->add($rules-><%= $rule['name'] %>(['<%= $field %>']<%= !empty($rule['extra']) ? ", '$rule[extra]'" : '' %>)); + <%- endforeach; %> + return $rules; + } +<% endif; %> +<% if ($connection !== 'default'): %> + + /** + * Returns the database connection name to use by default. + * + * @return string + */ + public static function defaultConnectionName() + { + return '<%= $connection %>'; + } +<% endif; %> +} diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/index.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/index.ctp new file mode 100644 index 0000000..112a40c --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/index.ctp @@ -0,0 +1,179 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +use Cake\Utility\Inflector; +use Cake\Utility\Hash; + +/* Enable for debug +echo "\n"; +*/ +$fields = collection($fields) + ->filter(function($field) use ($schema) { + return !in_array($schema->columnType($field), ['binary', 'text']) && $field != 'id'; + }) + ->take(7); +%> + +
+

') ?>

+ +<% + if ( $this->BakeExtra->hasFilters($modelClass) ): +%> + Form->create(null) . "\n" ?> +<% + endif; +%> + +<% if ( $this->BakeExtra->hasFilters($modelClass) ): %> + +<% foreach ( $this->BakeExtra->getFilters($modelClass) as $k => $filter): %> + +<% endforeach; %> + + +<% endif; %> + +<% foreach ($fields as $field): %> + +<% endforeach; %> + + + +<% if ( $this->BakeExtra->hasFilters($modelClass) ): %> + Form->end() . "\n" ?> +<% endif; %> + + as $<%= $singularVar %>): ?> + +<% foreach ($fields as $field) { + $isKey = false; + if (!empty($associations['BelongsTo'])) { + foreach ($associations['BelongsTo'] as $alias => $details) { + if ($field === $details['foreignKey']) { + $isKey = true; + if ( $this->BakeExtra->hasAction("view", $details['controller']) ) { +%> + +<% + } else { +%> + +<% + } + break; + } + } + } + if ($isKey !== true) { + if (!in_array($schema->columnType($field), ['integer', 'biginteger', 'decimal', 'float'])) { +%> + +<% + } else { +%> + +<% + } + } + } + + $pk = '$' . $singularVar . '->' . $primaryKey[0]; +%> + + + + +
+ Form->input('<%= $k %>', [ + 'placeholder' => __('<%= $filter["hint"] %>'), + 'empty' => __('<%= $filter["hint"] %>') + ]) ?> + + Form->button('Filter', ['type' => 'submit']) . "\n" ?> + Html->link('Reset', ['action' => 'index']) . "\n" ?> +
Paginator->sort('<%= $field %>') ?>
->has('<%= $details['property'] %>') ? $this->Html->link($<%= $singularVar %>-><%= $details['property'] %>-><%= $details['displayField'] %>, ['controller' => '<%= $details['controller'] %>', 'action' => 'view', $<%= $singularVar %>-><%= $details['property'] %>-><%= $details['primaryKey'][0] %>]) : '' ?>->has('<%= $details['property'] %>') ? h($<%= $singularVar %>-><%= $details['property'] %>-><%= $details['displayField'] %>) : '' ?>-><%= $field %>) ?>Number->format($<%= $singularVar %>-><%= $field %>) ?> +<% + if ($this->BakeExtra->hasAction("view", $modelClass)): +%> + Html->link(__('View'), ['action' => 'view', <%= $pk %>]) ?> +<% + endif; + if ($this->BakeExtra->hasAction("edit", $modelClass)): +%> + Html->link(__('Edit'), ['action' => 'edit', <%= $pk %>]) ?> +<% + endif; + if ($this->BakeExtra->hasAction("delete", $modelClass)): +%> + Form->postLink(__('Delete'), ['action' => 'delete', <%= $pk %>], ['confirm' => __('Are you sure you want to delete # {0}?', <%= $pk %>)]) ?> +<% + endif; +%> +
+
+
    + Paginator->prev('< ' . __('previous')) ?> + Paginator->numbers() ?> + Paginator->next(__('next') . ' >') ?> +
+
+ +
diff --git a/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/view.ctp b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/view.ctp new file mode 100644 index 0000000..8ab9916 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/Template/Bake/Template/view.ctp @@ -0,0 +1,215 @@ +<% +/** + * Copyright 2016 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +/** + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://cakephp.org CakePHP(tm) Project + * @since 0.1.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +use Cake\Utility\Inflector; + +$associations += ['BelongsTo' => [], 'HasOne' => [], 'HasMany' => [], 'BelongsToMany' => []]; +$immediateAssociations = $associations['BelongsTo'] + $associations['HasOne']; +$associationFields = collection($fields) + ->map(function($field) use ($immediateAssociations) { + foreach ($immediateAssociations as $alias => $details) { + if ($field === $details['foreignKey']) { + return [$field => $details]; + } + } + }) + ->filter() + ->reduce(function($fields, $value) { + return $fields + $value; + }, []); + +$groupedFields = collection($fields) + ->filter(function($field) use ($schema) { + return $schema->columnType($field) !== 'binary'; + }) + ->groupBy(function($field) use ($schema, $associationFields) { + $type = $schema->columnType($field); + if (isset($associationFields[$field])) { + return 'string'; + } + if (in_array($type, ['integer', 'float', 'decimal', 'biginteger'])) { + return 'number'; + } + if (in_array($type, ['date', 'time', 'datetime', 'timestamp'])) { + return 'date'; + } + return in_array($type, ['text', 'boolean']) ? $type : 'string'; + }) + ->toArray(); + +$groupedFields += ['number' => [], 'string' => [], 'boolean' => [], 'date' => [], 'text' => []]; +$pk = "\$$singularVar->{$primaryKey[0]}"; +%> + +
+

-><%= $displayField %>) ?>

+ +<% if ($groupedFields['string']) : %> +<% foreach ($groupedFields['string'] as $field) : %> +<% if (isset($associationFields[$field])) : + $details = $associationFields[$field]; +%> + + +<% + if ( $this->BakeExtra->hasAction("view", $details['controller']) ): +%> + +<% + else: +%> + +<% + endif; +%> + +<% else : %> + + + + +<% endif; %> +<% endforeach; %> +<% endif; %> +<% if ($groupedFields['number']) : %> +<% foreach ($groupedFields['number'] as $field) : %> + + + + +<% endforeach; %> +<% endif; %> +<% if ($groupedFields['date']) : %> +<% foreach ($groupedFields['date'] as $field) : %> + + + + +<% endforeach; %> +<% endif; %> +<% if ($groupedFields['boolean']) : %> +<% foreach ($groupedFields['boolean'] as $field) : %> + + + + +<% endforeach; %> +<% endif; %> +
') ?>->has('<%= $details['property'] %>') ? $this->Html->link($<%= $singularVar %>-><%= $details['property'] %>-><%= $details['displayField'] %>, ['controller' => '<%= $details['controller'] %>', 'action' => 'view', $<%= $singularVar %>-><%= $details['property'] %>-><%= $details['primaryKey'][0] %>]) : '' ?>-><%= $details['property'] %>-><%= $details['displayField'] %> ?>
') ?>-><%= $field %>) ?>
') ?>Number->format($<%= $singularVar %>-><%= $field %>) ?>
<%= "<%= __('" . Inflector::humanize($field) . "') %>" %>-><%= $field %>) ?>
') ?>-><%= $field %> ? __('Yes') : __('No'); ?>
+<% if ($groupedFields['text']) : %> +<% foreach ($groupedFields['text'] as $field) : %> +
+

') ?>

+ Text->autoParagraph(h($<%= $singularVar %>-><%= $field %>)); ?> +
+<% endforeach; %> +<% endif; %> +<% +$relations = $associations['HasMany'] + $associations['BelongsToMany']; +foreach ($relations as $alias => $details): + $otherSingularVar = Inflector::variable($alias); + $otherPluralHumanName = Inflector::humanize(Inflector::underscore($details['controller'])); + %> + +<% endforeach; %> +
diff --git a/generator/before-bake/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php b/generator/before-bake/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php new file mode 100644 index 0000000..69506a9 --- /dev/null +++ b/generator/before-bake/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php @@ -0,0 +1,65 @@ + + * Copyright 2016 Nicolas Goaziou + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see . +**/ +namespace CustomTheme\View\Helper; + +use Cake\View\Helper; +use Cake\Utility\Hash; + +class BakeExtraHelper extends Helper +{ + public function hello() + { + return "\n"; + } + + public function hasFilters($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $filters = $this->getFilters($controllerName); + return (is_array($filters) && count($filters) > 0); + } + + public function getFilters($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + return Hash::get($this->_config, "$controllerName.filters"); + } + + public function hasAction($action, $controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + return in_array($action, $this->getActions($controllerName)); + } + + public function getActions($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $actions = Hash::get($this->_config, "$controllerName.actions"); + if ( ! is_array($actions) ) { + $actions = Hash::get($this->_config, "default.actions"); + } + if ( ! is_array($actions) ) { + $actions = ['index', 'view', 'add', 'edit', 'delete']; + } + return $actions; + } + // TODO : modelClass or controllerName ? + public function getTitleOpts($modelClass) { + if ( ! is_string($modelClass) ) return FALSE; + return Hash::get($this->_config, "$modelClass.title"); + } +} -- cgit v1.1