PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Extensions/Controller/ExtensionsLocalesController.php

http://github.com/croogo/croogo
PHP | 243 lines | 150 code | 33 blank | 60 comment | 33 complexity | 438acbe8e1f00bdfa30917b2cf64d8a5 MD5 | raw file
Possible License(s): LGPL-2.1
  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. $locales = array();
  41. $folder =& new Folder;
  42. $paths = App::path('Locale');
  43. foreach ($paths as $path) {
  44. $folder->path = $path;
  45. $content = $folder->read();
  46. foreach ($content['0'] as $locale) {
  47. if (strstr($locale, '.') !== false) {
  48. continue;
  49. }
  50. if (!file_exists($path . $locale . DS . 'LC_MESSAGES' . DS . 'croogo.po')) {
  51. continue;
  52. }
  53. $locales[] = $locale;
  54. }
  55. }
  56. $this->set(compact('content', 'locales'));
  57. }
  58. /**
  59. * admin_activate
  60. *
  61. * @param string $locale
  62. * @return void
  63. */
  64. public function admin_activate($locale = null) {
  65. $poFile = $this->__getPoFile($locale);
  66. if ($locale == null || !$poFile) {
  67. $this->Session->setFlash(__d('croogo', 'Locale does not exist.'), 'flash', array('class' => 'error'));
  68. return $this->redirect(array('action' => 'index'));
  69. }
  70. $result = $this->Setting->write('Site.locale', $locale);
  71. if ($result) {
  72. $this->Session->setFlash(sprintf(__d('croogo', "Locale '%s' set as default"), $locale), 'flash', array('class' => 'success'));
  73. } else {
  74. $this->Session->setFlash(__d('croogo', 'Could not save Locale setting.'), 'flash', array('class' => 'error'));
  75. }
  76. return $this->redirect(array('action' => 'index'));
  77. }
  78. /**
  79. * admin_add
  80. *
  81. * @return void
  82. */
  83. public function admin_add() {
  84. $this->set('title_for_layout', __d('croogo', 'Upload a new locale'));
  85. if ($this->request->is('post') && !empty($this->request->data)) {
  86. $file = $this->request->data['Locale']['file'];
  87. unset($this->request->data['Locale']['file']);
  88. // get locale name
  89. $zip = zip_open($file['tmp_name']);
  90. $locale = null;
  91. if ($zip) {
  92. while ($zipEntry = zip_read($zip)) {
  93. $zipEntryName = zip_entry_name($zipEntry);
  94. if (strstr($zipEntryName, 'LC_MESSAGES')) {
  95. $zipEntryNameE = explode('/LC_MESSAGES', $zipEntryName);
  96. if (isset($zipEntryNameE['0'])) {
  97. $pathE = explode('/', $zipEntryNameE['0']);
  98. if (isset($pathE[count($pathE) - 1])) {
  99. $locale = $pathE[count($pathE) - 1];
  100. }
  101. }
  102. }
  103. }
  104. }
  105. zip_close($zip);
  106. if (!$locale) {
  107. $this->Session->setFlash(__d('croogo', 'Invalid locale.'), 'flash', array('class' => 'error'));
  108. return $this->redirect(array('action' => 'add'));
  109. }
  110. if (is_dir(APP . 'Locale' . DS . $locale)) {
  111. $this->Session->setFlash(__d('croogo', 'Locale already exists.'), 'flash', array('class' => 'error'));
  112. return $this->redirect(array('action' => 'add'));
  113. }
  114. // extract
  115. $zip = zip_open($file['tmp_name']);
  116. if ($zip) {
  117. while ($zipEntry = zip_read($zip)) {
  118. $zipEntryName = zip_entry_name($zipEntry);
  119. if (strstr($zipEntryName, $locale . '/')) {
  120. $zipEntryNameE = explode($locale . '/', $zipEntryName);
  121. if (isset($zipEntryNameE['1'])) {
  122. $path = APP . 'Locale' . DS . $locale . DS . str_replace('/', DS, $zipEntryNameE['1']);
  123. } else {
  124. $path = APP . 'Locale' . DS . $locale . DS;
  125. }
  126. if (substr($path, strlen($path) - 1) == DS) {
  127. // create directory
  128. mkdir($path);
  129. } else {
  130. // create file
  131. if (zip_entry_open($zip, $zipEntry, 'r')) {
  132. $fileContent = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));
  133. touch($path);
  134. $fh = fopen($path, 'w');
  135. fwrite($fh, $fileContent);
  136. fclose($fh);
  137. zip_entry_close($zipEntry);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. zip_close($zip);
  144. return $this->redirect(array('action' => 'index'));
  145. }
  146. }
  147. /**
  148. * admin_edit
  149. *
  150. * @param string $locale
  151. * @return void
  152. */
  153. public function admin_edit($locale = null) {
  154. $this->set('title_for_layout', sprintf(__d('croogo', 'Edit locale: %s'), $locale));
  155. if (!$locale) {
  156. $this->Session->setFlash(__d('croogo', 'Invalid locale.'), 'flash', array('class' => 'error'));
  157. return $this->redirect(array('action' => 'index'));
  158. }
  159. $poFile = $this->__getPoFile($locale);
  160. if (!$poFile) {
  161. $this->Session->setFlash(__d('croogo', 'The file %s does not exist.', 'croogo.po'), 'flash', array('class' => 'error'));
  162. return $this->redirect(array('action' => 'index'));
  163. }
  164. $file =& new File($poFile, true);
  165. $content = $file->read();
  166. if (!empty($this->request->data)) {
  167. // save
  168. if ($file->write($this->request->data['Locale']['content'])) {
  169. $this->Session->setFlash(__d('croogo', 'Locale updated successfully'), 'flash', array('class' => 'success'));
  170. return $this->redirect(array('action' => 'index'));
  171. }
  172. }
  173. $this->set(compact('locale', 'content'));
  174. }
  175. /**
  176. * admin_delete
  177. *
  178. * @param string $locale
  179. * @return void
  180. */
  181. public function admin_delete($locale = null) {
  182. $poFile = $this->__getPoFile($locale);
  183. if (!$poFile) {
  184. $this->Session->setFlash(__d('croogo', 'The file %s does not exist.', 'croogo.po'), 'flash', array('class' => 'error'));
  185. return $this->redirect(array('action' => 'index'));
  186. }
  187. $file =& new File($poFile, true);
  188. if ($file->delete()) {
  189. $this->Session->setFlash(__d('croogo', 'Locale deleted successfully.'), 'flash', array('class' => 'success'));
  190. } else {
  191. $this->Session->setFlash(__d('croogo', 'Local could not be deleted.'), 'flash', array('class' => 'error'));
  192. }
  193. return $this->redirect(array('action' => 'index'));
  194. }
  195. /**
  196. * Returns the path to the croogo.po file
  197. *
  198. * @param $locale
  199. */
  200. private function __getPoFile($locale) {
  201. $paths = App::path('Locale');
  202. foreach ($paths as $path) {
  203. $poFile = $path . $locale . DS . 'LC_MESSAGES' . DS . 'croogo.po';
  204. if (file_exists($poFile)) {
  205. return $poFile;
  206. }
  207. }
  208. return false;
  209. }
  210. }