From a27b88397a037332023a0b6473a4469bf75122d6 Mon Sep 17 00:00:00 2001 From: Daniel Pakuschewski Date: Tue, 31 Jul 2012 17:57:30 -0300 Subject: [PATCH 1/5] beforeSave should return true to no stop other callbacks --- Model/Behavior/I18nableBehavior.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Model/Behavior/I18nableBehavior.php b/Model/Behavior/I18nableBehavior.php index 84a6ae6..3791504 100644 --- a/Model/Behavior/I18nableBehavior.php +++ b/Model/Behavior/I18nableBehavior.php @@ -83,5 +83,6 @@ public function beforeSave($Model) { $Model->set(array($settings['languageField'] => $language)); } } + return true; } } From 4683f0e988b75b1d14642ef39890d3aaf4336c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20M=2E=20Gonz=C3=A1lez=20Mart=C3=ADn?= Date: Mon, 1 Feb 2016 10:42:00 +0000 Subject: [PATCH 2/5] fix contrib links --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 408e811..26f5308 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ Contributing ============ -This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugins). If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](http://cakedc.com/plugins) for detailed instructions. \ No newline at end of file +This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-standard). If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](http://cakedc.com/contribution-guidelines) for detailed instructions. From 1004382980a6a557f6a3783b18fd7f0becfffb34 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 29 Mar 2017 20:13:19 -0300 Subject: [PATCH 3/5] refs #40 updating the plugin to work with cakephp 2.9 --- Controller/I18nAppController.php | 2 + Controller/TranslationsController.php | 5 +- Lib/Routing/Route/I18nRoute.php | 4 +- Lib/Routing/Route/SluggableRoute.php | 41 ++++---- Model/Translation.php | 98 +++++++++++-------- .../Controller/TranslationsControllerTest.php | 12 ++- Test/Case/Model/TranslationTest.php | 8 +- 7 files changed, 101 insertions(+), 69 deletions(-) diff --git a/Controller/I18nAppController.php b/Controller/I18nAppController.php index bad4878..eab08ab 100644 --- a/Controller/I18nAppController.php +++ b/Controller/I18nAppController.php @@ -9,6 +9,8 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::uses('AppController', 'Controller'); + /** * I18n AppController * diff --git a/Controller/TranslationsController.php b/Controller/TranslationsController.php index 4cbaa89..1b647fc 100644 --- a/Controller/TranslationsController.php +++ b/Controller/TranslationsController.php @@ -105,16 +105,17 @@ protected function _pluginLoaded($plugin, $exception = true) { * @access public */ public function admin_index() { + $conditions = array(); if ($this->_pluginLoaded('Search', false)) { $this->Prg->commonProcess(); $conditions = $this->Translation->parseCriteria($this->passedArgs); - } else { - $conditions= array(); } + $this->Paginator->settings = array( 'search', 'conditions' => $conditions ); + $this->set('translations', $this->Paginator->paginate()); } diff --git a/Lib/Routing/Route/I18nRoute.php b/Lib/Routing/Route/I18nRoute.php index 3c9f0a5..cc3f4e1 100644 --- a/Lib/Routing/Route/I18nRoute.php +++ b/Lib/Routing/Route/I18nRoute.php @@ -128,9 +128,9 @@ protected function _shouldStripDefaultLanguageOnMatch() { public function getDefaultLanguage() { $lang = Configure::read('Config.languageOverride'); if (empty($lang)) { - $lang = Configure::read('Config.language'); - return $lang; + return Configure::read('Config.language'); } + return $lang; } diff --git a/Lib/Routing/Route/SluggableRoute.php b/Lib/Routing/Route/SluggableRoute.php index ae1fcfb..7d5fe7c 100644 --- a/Lib/Routing/Route/SluggableRoute.php +++ b/Lib/Routing/Route/SluggableRoute.php @@ -100,7 +100,7 @@ public function match($url) { public function slug($slug) { $str = $slug['_field']; if ($slug['_count'] > 1 || (isset($this->options['prependPk']) && $this->options['prependPk'])) { - $str = $slug['_pk'].' '.$str; + $str = $slug['_pk'] . ' ' . $str; } return $this->_slug($str); } @@ -129,10 +129,10 @@ protected function _slug($str, $replacement = '-') { */ public function getSlugs($modelName, $field = null) { $cacheConfig = $this->_initSluggerCache(); - if (!isset($this->{$modelName.'_slugs'})) { - $this->{$modelName.'_slugs'} = Cache::read($modelName.'_slugs', $cacheConfig); + if (!isset($this->{$modelName . '_slugs'})) { + $this->{$modelName . '_slugs'} = Cache::read($modelName.'_slugs', $cacheConfig); } - if (empty($this->{$modelName.'_slugs'})) { + if (empty($this->{$modelName . '_slugs'})) { $Model = ClassRegistry::init($modelName); if ($Model === false) { return false; @@ -142,21 +142,21 @@ public function getSlugs($modelName, $field = null) { } $slugs = $Model->find('all', array( 'fields' => array( - $Model->name.'.'.$Model->primaryKey, - $Model->name.'.'.$field, + $Model->name . '.' . $Model->primaryKey, + $Model->name . '.' . $field, ), 'recursive' => -1 )); $counts = $Model->find('all', array( 'fields' => array( - 'LOWER(TRIM('.$Model->name.'.'.$field.')) AS '.$field, + 'LOWER(TRIM(' . $Model->name . '.' . $field . ')) AS ' . $field, 'COUNT(*) AS count' ), 'group' => array( $field ) )); - $counts = Set::combine($counts, '{n}.0.'.$field, '{n}.0.count'); + $counts = Set::combine($counts, '{n}.0.' . $field, '{n}.0.count'); $listedSlugs = array(); foreach ($slugs as $pk => $fields) { $values = array( @@ -166,11 +166,11 @@ public function getSlugs($modelName, $field = null) { ); $listedSlugs[$fields[$Model->name][$Model->primaryKey]] = $this->slug($values); } - Cache::write($modelName.'_slugs', $listedSlugs, $cacheConfig); - $this->{$modelName.'_slugs'} = $listedSlugs; + Cache::write($modelName . '_slugs', $listedSlugs, $cacheConfig); + $this->{$modelName . '_slugs'} = $listedSlugs; } - - return $this->{$modelName.'_slugs'}; + + return $this->{$modelName . '_slugs'}; } /** @@ -184,10 +184,10 @@ public function invalidateCache($modelName, $id = null) { $cacheConfig = $this->_initSluggerCache(); if (is_null($id)) { - $result = Cache::delete($modelName.'_slugs', $cacheConfig); - unset($this->{$modelName.'_slugs'}); + $result = Cache::delete($modelName . '_slugs', $cacheConfig); + unset($this->{$modelName . '_slugs'}); } else { - $slugs = Cache::read($modelName.'_slugs', $cacheConfig); + $slugs = Cache::read($modelName . '_slugs', $cacheConfig); if ($slugs === false) { $result = false; } else { @@ -195,10 +195,10 @@ public function invalidateCache($modelName, $id = null) { if ($slugs[$id] === false) { unset($slugs[$id]); } - if (isset($this->{$modelName.'_slugs'}) && $slugs[$id] !== false) { - $this->{$modelName.'_slugs'}[$id] = $slugs[$id]; + if (isset($this->{$modelName . '_slugs'}) && $slugs[$id] !== false) { + $this->{$modelName . '_slugs'}[$id] = $slugs[$id]; } - $result = Cache::write($modelName.'_slugs', $slugs, $cacheConfig); + $result = Cache::write($modelName . '_slugs', $slugs, $cacheConfig); } } @@ -230,11 +230,11 @@ public function generateSlug($modelName, $id) { $slugField = $Model->displayField; } $text = $Model->field($slugField, array( - $Model->name.'.'.$Model->primaryKey => $id + $Model->name . '.' . $Model->primaryKey => $id )); if ($text !== false) { $count = $Model->find('count', array( - 'conditions' => array($Model->name.'.'.$slugField => $text) + 'conditions' => array($Model->name . '.' . $slugField => $text) )); $values = array('_field' => $text, '_count' => $count, '_pk' => $id); $slug = $this->slug($values); @@ -257,6 +257,7 @@ protected function _initSluggerCache() { 'duration' => '+1 days', 'prefix' => 'slugger_' )); + return 'Slugger'; } } diff --git a/Model/Translation.php b/Model/Translation.php index d08ff17..116f443 100644 --- a/Model/Translation.php +++ b/Model/Translation.php @@ -37,13 +37,29 @@ class Translation extends I18nAppModel { */ public $validate = array( 'locale' => array( - 'notempty' => array('rule' => array('notempty'), 'required' => true, 'allowEmpty' => false, 'message' => 'Please enter a Locale')), + 'rule' => array('notBlank'), + 'required' => true, + 'allowEmpty' => false, + 'message' => 'Please enter a Locale' + ), 'model' => array( - 'notempty' => array('rule' => array('notempty'), 'required' => true, 'allowEmpty' => false, 'message' => 'Please enter a Model')), + 'rule' => array('notBlank'), + 'required' => true, + 'allowEmpty' => false, + 'message' => 'Please enter a Model' + ), 'foreign_key' => array( - 'notempty' => array('rule' => array('notempty'), 'required' => true, 'allowEmpty' => false, 'message' => 'Please enter a Foreign Key')), + 'rule' => array('notBlank'), + 'required' => true, + 'allowEmpty' => false, + 'message' => 'Please enter a Foreign Key' + ), 'field' => array( - 'notempty' => array('rule' => array('notempty'), 'required' => true, 'allowEmpty' => false, 'message' => 'Please enter a Field')), + 'rule' => array('notBlank'), + 'required' => true, + 'allowEmpty' => false, + 'message' => 'Please enter a Field' + ), ); /** @@ -106,12 +122,11 @@ public function add($data = null) { $this->create(); $result = $this->save($data); if ($result !== false) { - $this->data = array_merge($data, $result); - return true; + $this->data = Hash::merge($data, $result); + return $return; } else { throw new OutOfBoundsException(__('Could not save the translation, please check your inputs.', true)); } - return $return; } } @@ -128,7 +143,8 @@ public function edit($id = null, $data = null) { $translation = $this->find('first', array( 'conditions' => array( "{$this->alias}.{$this->primaryKey}" => $id, - ))); + ) + )); if (empty($translation)) { throw new OutOfBoundsException(__('Invalid Translation', true)); @@ -141,12 +157,12 @@ public function edit($id = null, $data = null) { if ($result) { $this->data = $result; return true; - } else { - return $data; } - } else { - return $translation; + + return $data; } + + return $translation; } /** @@ -175,28 +191,29 @@ public function edit_multi($model, $foreignKey, $data = null) { // $this->set($translation); if (!empty($data[$this->alias])) { - foreach ($data[$this->alias] as $locale => $fields) { - foreach ($fields as $field => $_data) { - if (!empty($_data['id'])) { - $record = $this->read(null, $_data['id']); - $record[$this->alias]['content'] = $_data['content']; - $this->save($record); - } else { - $record = array('Translation' => array( - 'model' => $model, - 'foreign_key' => $foreignKey, - 'locale' => $locale, - 'field' => $field, - 'content' => $_data['content'])); - $this->create($record); - $this->save($record); - } + foreach ($data[$this->alias] as $locale => $fields) { + if (!empty($fields['id'])) { + $record = $this->read(null, $_data['id']); + $record[$this->alias]['content'] = $_data['content']; + $this->set($record); + $this->save($record); + } else { + $record = array('Translation' => array( + 'model' => $model, + 'foreign_key' => $foreignKey, + 'locale' => $fields['locale'], + 'field' => $fields['field'], + 'content' => $fields['content'] + )); + $this->create($record); + $this->save($record); } } + return true; - } else { - return $translations; } + + return $translations; } /** @@ -209,7 +226,9 @@ public function edit_multi($model, $foreignKey, $data = null) { public function view($id = null) { $translation = $this->find('first', array( 'conditions' => array( - "{$this->alias}.{$this->primaryKey}" => $id))); + "{$this->alias}.{$this->primaryKey}" => $id + ) + )); if (empty($translation)) { throw new OutOfBoundsException(__('Invalid Translation', true)); @@ -225,13 +244,14 @@ public function view($id = null) { * @param array $data, controller post data usually $this->data * @return boolean True on success * @throws OutOfBoundsException If the element does not exists + * @throws Exception If the confirmation was not send */ public function validateAndDelete($id = null, $data = array()) { $translation = $this->find('first', array( 'conditions' => array( "{$this->alias}.{$this->primaryKey}" => $id, - )) - ); + ) + )); if (empty($translation)) { throw new OutOfBoundsException(__('Invalid Translation', true)); @@ -242,8 +262,9 @@ public function validateAndDelete($id = null, $data = array()) { $data['Translation']['id'] = $id; $tmp = $this->validate; $this->validate = array( - 'id' => array('rule' => 'notEmpty'), - 'confirm' => array('rule' => '[1]')); + 'id' => array('rule' => 'notBlank'), + 'confirm' => array('rule' => '[1]') + ); $this->set($data); if ($this->validates()) { @@ -259,14 +280,13 @@ public function validateAndDelete($id = null, $data = array()) { /** * Perform search request * - * @param string $state - * @param array $query - * @param array $results + * @param string $state query state + * @param array $query array + * @param array $results results * @return array */ protected function _findSearch($state, $query, $results = array()) { if ($state == 'before') { - //$query = Set::merge($defaults, $query); if (!empty($query['operation']) && $query['operation'] === 'count') { unset($query['limit']); $query = $this->_findCount('before', $query, $results); diff --git a/Test/Case/Controller/TranslationsControllerTest.php b/Test/Case/Controller/TranslationsControllerTest.php index 2ae0290..57067d5 100644 --- a/Test/Case/Controller/TranslationsControllerTest.php +++ b/Test/Case/Controller/TranslationsControllerTest.php @@ -21,6 +21,10 @@ class TranslationsControllerTestCase extends CakeTestCase { */ protected $_testsToRun = array(); + public $fixtures = array( + 'plugin.i18n.i18n' + ); + /** * Start Test callback * @@ -28,15 +32,15 @@ class TranslationsControllerTestCase extends CakeTestCase { */ public function setUp() { parent::setUp(); - $this->Translations = new TranslationsController(); + $this->Translations = new TranslationsController(new CakeRequest(), new CakeResponse()); $this->Translations->constructClasses(); $this->Translations->Prg->initialize($this->Translations); $this->Translations->params = array( 'named' => array(), 'pass' => array(), - 'url' => array()); - $fixture = new I18nFixture(); - $this->record = array('Translation' => $fixture->records[0]); + 'url' => array() + ); + $this->record = $this->Translations->Translation->find('first'); } /** diff --git a/Test/Case/Model/TranslationTest.php b/Test/Case/Model/TranslationTest.php index 12c1d59..4da56d8 100644 --- a/Test/Case/Model/TranslationTest.php +++ b/Test/Case/Model/TranslationTest.php @@ -138,7 +138,9 @@ public function testValidateAndDelete() { try { $postData = array( 'Translation' => array( - 'confirm' => 0)); + 'confirm' => 0 + ) + ); $result = $this->Translation->validateAndDelete('translation-1', $postData); } catch (Exception $e) { $this->assertEquals($e->getMessage(), 'You need to confirm to delete this Translation'); @@ -146,7 +148,9 @@ public function testValidateAndDelete() { $postData = array( 'Translation' => array( - 'confirm' => 1)); + 'confirm' => 1 + ) + ); $result = $this->Translation->validateAndDelete('translation-1', $postData); $this->assertTrue($result); } From 6701dfb8b2a018887377f1bcb089ad00479ea0ed Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 29 Mar 2017 20:29:33 -0300 Subject: [PATCH 4/5] refs #40 updating travis yml --- .travis.yml | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2f4174d..c72f91d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.3 - - 5.4 - 5.5 - + - 5.6 + - 7.0 env: global: - PLUGIN_NAME=I18n @@ -16,18 +15,17 @@ env: matrix: include: - - php: 5.3 + - php: 5.5 env: - - CAKE_VERSION=master - - php: 5.4 + - CAKE_VERSION=2.7 + - php: 5.6 env: - - CAKE_VERSION=master - - php: 5.5 + - CAKE_VERSION=2.7 + - php: 7.0 env: - - CAKE_VERSION=master - + - CAKE_VERSION=2.9 before_script: - - git clone https://github.com/burzum/travis.git --depth 1 ../travis + - git clone https://github.com/steinkel/travis.git --depth 1 ../travis - ../travis/before_script.sh - if [ "$PHPCS" != 1 ]; then echo " From ae1d3667e3d06f93ff835c7fb4b92864c168a05d Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 29 Mar 2017 21:10:09 -0300 Subject: [PATCH 5/5] refs #40 adding a validation to check if Search plugin is loaded otherwise do not use it --- Test/Case/Controller/TranslationsControllerTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Test/Case/Controller/TranslationsControllerTest.php b/Test/Case/Controller/TranslationsControllerTest.php index 57067d5..5d38cb8 100644 --- a/Test/Case/Controller/TranslationsControllerTest.php +++ b/Test/Case/Controller/TranslationsControllerTest.php @@ -34,7 +34,9 @@ public function setUp() { parent::setUp(); $this->Translations = new TranslationsController(new CakeRequest(), new CakeResponse()); $this->Translations->constructClasses(); - $this->Translations->Prg->initialize($this->Translations); + if (CakePlugin::loaded('Search')) { + $this->Translations->Prg->initialize($this->Translations); + } $this->Translations->params = array( 'named' => array(), 'pass' => array(),