PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Extensions/Controller/ExtensionsLocalesController.php

https://github.com/kareypowell/croogo
PHP | 213 lines | 131 code | 27 blank | 55 comment | 31 complexity | de54b0f12340084e6b0bca728b4c97cb MD5 | raw file
  1. <?php
  2. App::uses('File', 'Utility');
  3. App::uses('Folder', 'Utility');
  4. App::uses('ExtensionsAppController', 'Extensions.Controller');
  5. /**
  6. * Extensions Locales Controller
  7. *
  8. * @category Controller
  9. * @package Croogo.Extensions.Controller
  10. * @version 1.0
  11. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class ExtensionsLocalesController extends ExtensionsAppController {
  16. /**
  17. * Controller name
  18. *
  19. * @var string
  20. * @access public
  21. */
  22. public $name = 'ExtensionsLocales';
  23. /**
  24. * Models used by the Controller
  25. *
  26. * @var array
  27. * @access public
  28. */
  29. public $uses = array(
  30. 'Settings.Setting',
  31. 'Users.User',
  32. );
  33. /**
  34. * admin_index
  35. *
  36. * @return void
  37. */
  38. public function admin_index() {
  39. $this->set('title_for_layout', __d('croogo', 'Locales'));
  40. $folder =& new Folder;
  41. $folder->path = APP . 'Locale';
  42. $content = $folder->read();
  43. $locales = $content['0'];
  44. foreach ($locales as $i => $locale) {
  45. if (strstr($locale, '.') !== false) {
  46. unset($locales[$i]);
  47. }
  48. }
  49. $this->set(compact('content', 'locales'));
  50. }
  51. /**
  52. * admin_activate
  53. *
  54. * @param string $locale
  55. * @return void
  56. */
  57. public function admin_activate($locale = null) {
  58. if ($locale == null || !is_dir(APP . 'Locale' . DS . $locale)) {
  59. $this->Session->setFlash(__d('croogo', 'Locale does not exist.'), 'default', array('class' => 'error'));
  60. return $this->redirect(array('action' => 'index'));
  61. }
  62. $result = $this->Setting->write('Site.locale', $locale);
  63. if ($result) {
  64. $this->Session->setFlash(sprintf(__d('croogo', "Locale '%s' set as default"), $locale), 'default', array('class' => 'success'));
  65. } else {
  66. $this->Session->setFlash(__d('croogo', 'Could not save Locale setting.'), 'default', array('class' => 'error'));
  67. }
  68. return $this->redirect(array('action' => 'index'));
  69. }
  70. /**
  71. * admin_add
  72. *
  73. * @return void
  74. */
  75. public function admin_add() {
  76. $this->set('title_for_layout', __d('croogo', 'Upload a new locale'));
  77. if ($this->request->is('post') && !empty($this->request->data)) {
  78. $file = $this->request->data['Locale']['file'];
  79. unset($this->request->data['Locale']['file']);
  80. // get locale name
  81. $zip = zip_open($file['tmp_name']);
  82. $locale = null;
  83. if ($zip) {
  84. while ($zipEntry = zip_read($zip)) {
  85. $zipEntryName = zip_entry_name($zipEntry);
  86. if (strstr($zipEntryName, 'LC_MESSAGES')) {
  87. $zipEntryNameE = explode('/LC_MESSAGES', $zipEntryName);
  88. if (isset($zipEntryNameE['0'])) {
  89. $pathE = explode('/', $zipEntryNameE['0']);
  90. if (isset($pathE[count($pathE) - 1])) {
  91. $locale = $pathE[count($pathE) - 1];
  92. }
  93. }
  94. }
  95. }
  96. }
  97. zip_close($zip);
  98. if (!$locale) {
  99. $this->Session->setFlash(__d('croogo', 'Invalid locale.'), 'default', array('class' => 'error'));
  100. return $this->redirect(array('action' => 'add'));
  101. }
  102. if (is_dir(APP . 'Locale' . DS . $locale)) {
  103. $this->Session->setFlash(__d('croogo', 'Locale already exists.'), 'default', array('class' => 'error'));
  104. return $this->redirect(array('action' => 'add'));
  105. }
  106. // extract
  107. $zip = zip_open($file['tmp_name']);
  108. if ($zip) {
  109. while ($zipEntry = zip_read($zip)) {
  110. $zipEntryName = zip_entry_name($zipEntry);
  111. if (strstr($zipEntryName, $locale . '/')) {
  112. $zipEntryNameE = explode($locale . '/', $zipEntryName);
  113. if (isset($zipEntryNameE['1'])) {
  114. $path = APP . 'Locale' . DS . $locale . DS . str_replace('/', DS, $zipEntryNameE['1']);
  115. } else {
  116. $path = APP . 'Locale' . DS . $locale . DS;
  117. }
  118. if (substr($path, strlen($path) - 1) == DS) {
  119. // create directory
  120. mkdir($path);
  121. } else {
  122. // create file
  123. if (zip_entry_open($zip, $zipEntry, 'r')) {
  124. $fileContent = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));
  125. touch($path);
  126. $fh = fopen($path, 'w');
  127. fwrite($fh, $fileContent);
  128. fclose($fh);
  129. zip_entry_close($zipEntry);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. zip_close($zip);
  136. return $this->redirect(array('action' => 'index'));
  137. }
  138. }
  139. /**
  140. * admin_edit
  141. *
  142. * @param string $locale
  143. * @return void
  144. */
  145. public function admin_edit($locale = null) {
  146. $this->set('title_for_layout', sprintf(__d('croogo', 'Edit locale: %s'), $locale));
  147. if (!$locale) {
  148. $this->Session->setFlash(__d('croogo', 'Invalid locale.'), 'default', array('class' => 'error'));
  149. return $this->redirect(array('action' => 'index'));
  150. }
  151. $poFile = APP . 'Locale' . DS . $locale . DS . 'LC_MESSAGES' . DS . 'croogo.po';
  152. if (!file_exists($poFile)) {
  153. $this->Session->setFlash(__d('croogo', 'The file %s does not exist.', basename($poFile)), 'default', array('class' => 'error'));
  154. return $this->redirect(array('action' => 'index'));
  155. }
  156. $file =& new File($poFile, true);
  157. $content = $file->read();
  158. if (!empty($this->request->data)) {
  159. // save
  160. if ($file->write($this->request->data['Locale']['content'])) {
  161. $this->Session->setFlash(__d('croogo', 'Locale updated successfully'), 'default', array('class' => 'success'));
  162. return $this->redirect(array('action' => 'index'));
  163. }
  164. }
  165. $this->set(compact('locale', 'content'));
  166. }
  167. /**
  168. * admin_delete
  169. *
  170. * @param string $locale
  171. * @return void
  172. */
  173. public function admin_delete($locale = null) {
  174. if (!$locale) {
  175. $this->Session->setFlash(__d('croogo', 'Invalid locale'), 'default', array('class' => 'error'));
  176. return $this->redirect(array('action' => 'index'));
  177. }
  178. $folder =& new Folder;
  179. if ($folder->delete(APP . 'Locale' . DS . $locale)) {
  180. $this->Session->setFlash(__d('croogo', 'Locale deleted successfully.'), 'default', array('class' => 'success'));
  181. } else {
  182. $this->Session->setFlash(__d('croogo', 'Local could not be deleted.'), 'default', array('class' => 'error'));
  183. }
  184. return $this->redirect(array('action' => 'index'));
  185. }
  186. }