/application/plugins/i18n/models/I18nLocale.class.php

https://github.com/dbernar1/Project-Pier · PHP · 323 lines · 108 code · 39 blank · 176 comment · 16 complexity · 6e2d303ac1c1b2f1eec70da003109ff0 MD5 · raw file

  1. <?php
  2. /**
  3. * I18nLocale class
  4. *
  5. */
  6. class I18nLocale extends BaseI18nLocale {
  7. /**
  8. * This type of object is taggable
  9. *
  10. * @var boolean
  11. */
  12. protected $is_taggable = false;
  13. /**
  14. * This type of object is searchable
  15. *
  16. * @var boolean
  17. */
  18. protected $is_searchable = false;
  19. /**
  20. * Array of searchable columns
  21. *
  22. * @var array
  23. */
  24. protected $searchable_columns = array('name', 'description');
  25. /**
  26. * This type of object is commentable
  27. *
  28. * @var boolean
  29. */
  30. protected $is_commentable = false;
  31. /**
  32. * This type of object is a file container
  33. *
  34. * @var boolean
  35. */
  36. protected $is_file_container = false;
  37. /**
  38. * This type of object is subscribable
  39. *
  40. * @var boolean
  41. */
  42. protected $is_subscribable = false;
  43. /**
  44. * Can add new locale
  45. *
  46. * @param void
  47. * @return null
  48. */
  49. function canAdd(User $user) {
  50. return $user->isAdministrator();
  51. }
  52. /**
  53. * Can edit locale
  54. *
  55. * @param void
  56. * @return null
  57. */
  58. function canEdit(User $user) {
  59. return $user->isAdministrator() || $user->getId() == $this->getEditor();
  60. }
  61. /**
  62. * Can delete locale
  63. *
  64. * @param void
  65. * @return null
  66. */
  67. function canDelete(User $user) {
  68. return $user->isAdministrator() || $user->getId() == $this->getEditor();
  69. }
  70. /**
  71. * Does user have view access
  72. *
  73. * @param void
  74. * @return boolean
  75. */
  76. function canView(User $user) {
  77. if ($user->isAdministrator() || $user->isMemberOfOwnerCompany()) {
  78. return true;
  79. } // if
  80. if ($user->getId() == $this->getEditor()) {
  81. return true;
  82. } // if
  83. return false;
  84. } // canView
  85. // ---------------------------------------------------
  86. // Loading
  87. // ---------------------------------------------------
  88. /**
  89. * copyValues
  90. *
  91. * @param boolean $replace Replace all values with the new values
  92. * @param integer $id Id of the locale to copy the values from
  93. * @return boolean
  94. */
  95. function copyValues($id, $replace) {
  96. return I18nLocaleValues::instance()->copy($id, $this->getId(), $replace);
  97. }
  98. /**
  99. * loadValues
  100. *
  101. * @param boolean $replace Replace all values with the new values
  102. * @param integer $locale Locale to use
  103. * @return boolean
  104. */
  105. function loadValues($locale, $replace) {
  106. return I18nLocaleValues::instance()->import($this->getId(), $locale, $replace);
  107. }
  108. /**
  109. * Return locale value identified by name
  110. *
  111. * @param string name
  112. * @return string
  113. */
  114. function getValue($name) {
  115. return I18nLocaleValues::instance()->getLocaleValue($this->getId(), $name);
  116. } // getEditUrl
  117. // ---------------------------------------------------
  118. // Logo
  119. // ---------------------------------------------------
  120. /**
  121. * Set logo
  122. *
  123. * @param string $source Source file
  124. * @param integer $max_width
  125. * @param integer $max_height
  126. * @param boolean $save Save object when done
  127. * @return null
  128. */
  129. function setLogo($source, $max_width = 50, $max_height = 50, $save = true) {
  130. if (!is_readable($source)) {
  131. return false;
  132. }
  133. do {
  134. $temp_file = ROOT . '/cache/' . sha1(uniqid(rand(), true));
  135. } while (is_file($temp_file));
  136. try {
  137. Env::useLibrary('simplegd');
  138. $image = new SimpleGdImage($source);
  139. $thumb = $image->scale($max_width, $max_height, SimpleGdImage::BOUNDARY_DECREASE_ONLY, false);
  140. $thumb->saveAs($temp_file, IMAGETYPE_PNG);
  141. $public_filename = PublicFiles::addFile($temp_file, 'png');
  142. if ($public_filename) {
  143. $this->setLogoFile($public_filename);
  144. if ($save) {
  145. $this->save();
  146. } // if
  147. } // if
  148. $result = true;
  149. } catch(Exception $e) {
  150. $result = false;
  151. } // try
  152. // Cleanup
  153. if (!$result && $public_filename) {
  154. PublicFiles::deleteFile($public_filename);
  155. } // if
  156. @unlink($temp_file);
  157. return $result;
  158. } // setLogo
  159. /**
  160. * Delete logo
  161. *
  162. * @param void
  163. * @return null
  164. */
  165. function deleteLogo() {
  166. if ($this->hasLogo()) {
  167. PublicFiles::deleteFile($this->getLogoFile());
  168. $this->setLogoFile('');
  169. } // if
  170. } // deleteLogo
  171. /**
  172. * Returns path of image. This function will not check if file really exists
  173. *
  174. * @access public
  175. * @param void
  176. * @return string
  177. */
  178. function getLogoPath() {
  179. return PublicFiles::getFilePath($this->getLogoFile());
  180. } // getLogoPath
  181. /**
  182. * description
  183. *
  184. * @access public
  185. * @param void
  186. * @return string
  187. */
  188. function getLogoUrl() {
  189. return $this->hasLogo() ? PublicFiles::getFileUrl($this->getLogoFile()) : get_image_url('logo.gif');
  190. } // getLogoUrl
  191. /**
  192. * Returns true if logo specified and logo file exists
  193. *
  194. * @access public
  195. * @param void
  196. * @return boolean
  197. */
  198. function hasLogo() {
  199. return trim($this->getLogoFile()) && is_file($this->getLogoPath());
  200. } // hasLogo
  201. /**
  202. * Return edit locale URL
  203. *
  204. * @param void
  205. * @return string
  206. */
  207. function getEditUrl() {
  208. return get_url('i18n', 'edit_locale', array('id' => $this->getId()));
  209. } // getEditUrl
  210. /**
  211. * Return edit locale logo URL
  212. *
  213. * @param void
  214. * @return string
  215. */
  216. function getEditLogoUrl() {
  217. return get_url('i18n', 'edit_logo', array('id' => $this->getId()));
  218. } // getEditLogoUrl
  219. /**
  220. * Return edit locale values URL
  221. *
  222. * @param void
  223. * @return string
  224. */
  225. function getEditValuesUrl() {
  226. return get_url('i18n', 'edit_values', array('id' => $this->getId()));
  227. } // getEditValuesUrl
  228. /**
  229. * Return load locale values URL
  230. *
  231. * @param void
  232. * @return string
  233. */
  234. function getLoadValuesUrl() {
  235. return get_url('i18n', 'load_values', array('id' => $this->getId()));
  236. } // getLoadValuesUrl
  237. /**
  238. * Return delete locale URL
  239. *
  240. * @param void
  241. * @return string
  242. */
  243. function getDeleteUrl() {
  244. return get_url('i18n', 'delete_locale', array('id' => $this->getId()));
  245. } // getEditUrl
  246. /**
  247. * Return delete locale logo URL
  248. *
  249. * @param void
  250. * @return string
  251. */
  252. function getDeleteLogoUrl() {
  253. return get_url('i18n', 'delete_logo', array('id' => $this->getId()));
  254. } // getDeleteLogoUrl
  255. /**
  256. * Return object name
  257. *
  258. * @param void
  259. * @return string
  260. */
  261. function getObjectName() {
  262. return $this->getName();
  263. }
  264. /**
  265. * Return object type name
  266. *
  267. * @param void
  268. * @return string
  269. */
  270. function getObjectTypeName() {
  271. return lang('i18n');
  272. } // getObjectTypeName
  273. /**
  274. * Return object URL
  275. *
  276. * @access public
  277. * @param void
  278. * @return string
  279. */
  280. function getObjectUrl() {
  281. return get_url('i18n', 'index', array());
  282. } // getObjectUrl
  283. } // I18nLocale
  284. ?>