PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Translate/Controller/TranslateController.php

https://github.com/kareypowell/croogo
PHP | 234 lines | 167 code | 23 blank | 44 comment | 24 complexity | e0cd09e1dd3e90b708ca4e6e6953b638 MD5 | raw file
  1. <?php
  2. App::uses('TranslateAppController', 'Translate.Controller');
  3. /**
  4. * Translate Controller
  5. *
  6. * @category Translate.Controller
  7. * @package Croogo.Translate.Controller
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class TranslateController extends TranslateAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Translate';
  21. /**
  22. * Models used by the Controller
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $uses = array(
  28. 'Settings.Setting',
  29. 'Settings.Language',
  30. );
  31. /**
  32. * admin_index
  33. *
  34. * @param integer $id
  35. * @param string $modelAlias
  36. * @return void
  37. */
  38. public function admin_index($id = null, $modelAlias = null) {
  39. if ($id == null || $modelAlias == null) {
  40. $this->Session->setFlash(__d('croogo', 'Invalid ID.'), 'default', array('class' => 'error'));
  41. return $this->redirect(array(
  42. 'plugin' => null,
  43. 'controller' => Inflector::pluralize($modelAlias),
  44. 'action' => 'index',
  45. ));
  46. }
  47. $config = Configure::read('Translate.models.' . $modelAlias);
  48. list($plugin, $modelAlias) = pluginSplit($config['translateModel']);
  49. if (!is_array($config)) {
  50. $this->Session->setFlash(__d('croogo', 'Invalid model.'), 'default', array('class' => 'error'));
  51. return $this->redirect(array(
  52. 'plugin' => $plugin,
  53. 'controller' => Inflector::pluralize($modelAlias),
  54. 'action' => 'index',
  55. ));
  56. }
  57. $model = ClassRegistry::init($config['translateModel']);
  58. $displayField = $model->displayField;
  59. $record = $model->findById($id);
  60. if (!isset($record[$modelAlias]['id'])) {
  61. $this->Session->setFlash(__d('croogo', 'Invalid record.'), 'default', array('class' => 'error'));
  62. return $this->redirect(array(
  63. 'plugin' => $plugin,
  64. 'controller' => Inflector::pluralize($modelAlias),
  65. 'action' => 'index',
  66. ));
  67. }
  68. $this->set('title_for_layout', sprintf(__d('croogo', 'Translations: %s'), $record[$modelAlias][$displayField]));
  69. $runtimeModel = $model->translateModel();
  70. $runtimeModelAlias = $runtimeModel->alias;
  71. $translations = $runtimeModel->find('all', array(
  72. 'conditions' => array(
  73. $runtimeModelAlias . '.model' => $modelAlias,
  74. $runtimeModelAlias . '.foreign_key' => $id,
  75. $runtimeModelAlias . '.field' => 'title',
  76. ),
  77. ));
  78. $this->set(compact('runtimeModelAlias', 'translations', 'record', 'modelAlias', 'displayField', 'id'));
  79. }
  80. /**
  81. * admin_edit
  82. *
  83. * @param integer $id
  84. * @param string $modelAlias
  85. * @return void
  86. */
  87. public function admin_edit($id = null, $modelAlias = null) {
  88. if (!$id && empty($this->request->data)) {
  89. $this->Session->setFlash(__d('croogo', 'Invalid ID.'), 'default', array('class' => 'error'));
  90. return $this->redirect(array(
  91. 'plugin' => null,
  92. 'controller' => Inflector::pluralize($modelAlias),
  93. 'action' => 'index',
  94. ));
  95. }
  96. if (!isset($this->request->params['named']['locale'])) {
  97. $this->Session->setFlash(__d('croogo', 'Invalid locale'), 'default', array('class' => 'error'));
  98. return $this->redirect(array(
  99. 'plugin' => null,
  100. 'controller' => Inflector::pluralize($modelAlias),
  101. 'action' => 'index',
  102. ));
  103. }
  104. $config = Configure::read('Translate.models.' . $modelAlias);
  105. list($plugin, $modelAlias) = pluginSplit($config['translateModel']);
  106. $language = $this->Language->find('first', array(
  107. 'conditions' => array(
  108. 'Language.alias' => $this->request->params['named']['locale'],
  109. 'Language.status' => 1,
  110. ),
  111. ));
  112. if (!isset($language['Language']['id'])) {
  113. $this->Session->setFlash(__d('croogo', 'Invalid Language'), 'default', array('class' => 'error'));
  114. return $this->redirect(array(
  115. 'plugin' => $plugin,
  116. 'controller' => Inflector::pluralize($modelAlias),
  117. 'action' => 'index',
  118. ));
  119. }
  120. $model = ClassRegistry::init($config['translateModel']);
  121. $displayField = $model->displayField;
  122. $record = $model->findById($id);
  123. if (!isset($record[$modelAlias]['id'])) {
  124. $this->Session->setFlash(__d('croogo', 'Invalid record.'), 'default', array('class' => 'error'));
  125. return $this->redirect(array(
  126. 'plugin' => $plugin,
  127. 'controller' => Inflector::pluralize($modelAlias),
  128. 'action' => 'index',
  129. ));
  130. }
  131. $this->set('title_for_layout', sprintf(__d('croogo', 'Translate content: %s (%s)'), $language['Language']['title'], $language['Language']['native']));
  132. $model->id = $id;
  133. $model->locale = $this->request->params['named']['locale'];
  134. $fields = $model->getTranslationFields();
  135. if (!empty($this->request->data)) {
  136. if ($model->saveTranslation($this->request->data)) {
  137. $this->Session->setFlash(__d('croogo', 'Record has been translated'), 'default', array('class' => 'success'));
  138. $redirect = array(
  139. 'action' => 'index',
  140. $id,
  141. $modelAlias,
  142. );
  143. if (isset($this->request->data['apply'])) {
  144. $redirect['action'] = 'edit';
  145. $redirect['locale'] = $model->locale;
  146. }
  147. return $this->redirect($redirect);
  148. } else {
  149. $this->Session->setFlash(__d('croogo', 'Record could not be translated. Please, try again.'), 'default', array('class' => 'error'));
  150. }
  151. }
  152. if (empty($this->request->data)) {
  153. $this->request->data = $model->read(null, $id);
  154. }
  155. $this->set(compact('fields', 'language', 'modelAlias', 'displayField', 'id'));
  156. }
  157. /**
  158. * admin_delete
  159. *
  160. * @param integer $id
  161. * @param string $modelAlias
  162. * @param string $locale
  163. * @return void
  164. */
  165. public function admin_delete($id = null, $modelAlias = null, $locale = null) {
  166. if ($locale == null || $id == null) {
  167. $this->Session->setFlash(__d('croogo', 'Invalid Locale or ID'), 'default', array('class' => 'error'));
  168. return $this->redirect(array(
  169. 'plugin' => null,
  170. 'controller' => Inflector::pluralize($modelAlias),
  171. 'action' => 'index',
  172. ));
  173. }
  174. $config = Configure::read('Translate.models.' . $modelAlias);
  175. list($plugin, $modelAlias) = pluginSplit($config['translateModel']);
  176. if (!is_array($config)) {
  177. $this->Session->setFlash(__d('croogo', 'Invalid model.'), 'default', array('class' => 'error'));
  178. return $this->redirect(array(
  179. 'plugin' => $plugin,
  180. 'controller' => Inflector::pluralize($modelAlias),
  181. 'action' => 'index',
  182. ));
  183. }
  184. $model = ClassRegistry::init($config['translateModel']);
  185. $record = $model->findById($id);
  186. if (!isset($record[$modelAlias]['id'])) {
  187. $this->Session->setFlash(__d('croogo', 'Invalid record.'), 'default', array('class' => 'error'));
  188. return $this->redirect(array(
  189. 'plugin' => $plugin,
  190. 'controller' => Inflector::pluralize($modelAlias),
  191. 'action' => 'index',
  192. ));
  193. }
  194. $runtimeModel = $model->translateModel();
  195. $runtimeModelAlias = $runtimeModel->alias;
  196. if ($runtimeModel->deleteAll(array(
  197. $runtimeModelAlias . '.model' => $modelAlias,
  198. $runtimeModelAlias . '.foreign_key' => $id,
  199. $runtimeModelAlias . '.locale' => $locale,
  200. ))) {
  201. $this->Session->setFlash(__d('croogo', 'Translation for the locale deleted successfully.'), 'default', array('class' => 'success'));
  202. } else {
  203. $this->Session->setFlash(__d('croogo', 'Translation for the locale could not be deleted.'), 'default', array('class' => 'error'));
  204. }
  205. return $this->redirect(array(
  206. 'action' => 'index',
  207. $id,
  208. $modelAlias,
  209. ));
  210. }
  211. }