PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_zoo/helpers/zoo.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 313 lines | 151 code | 38 blank | 124 comment | 59 complexity | 91787f0819e937242ddd032d93edd0c8 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * ZOOs generals helper class.
  10. *
  11. * @package Component.Helpers
  12. * @since 2.0
  13. */
  14. class ZooHelper extends AppHelper {
  15. /**
  16. * The current application
  17. * @var Application
  18. */
  19. protected $_application;
  20. /**
  21. * The current version
  22. * @var string
  23. */
  24. protected $_version;
  25. /**
  26. * user groups / view levels
  27. * @var array
  28. */
  29. protected $_groups;
  30. /**
  31. * Returns a reference to the currently active Application object.
  32. *
  33. * @return Application
  34. * @since 2.0
  35. */
  36. public function getApplication() {
  37. // check if application object already exists
  38. if (isset($this->_application)) {
  39. return $this->_application;
  40. }
  41. // get joomla and application table
  42. $joomla = $this->app->system->application;
  43. $table = $this->app->table->application;
  44. // handle admin
  45. if ($joomla->isAdmin()) {
  46. // create application from user state, or search for default
  47. $id = $joomla->getUserState('com_zooapplication');
  48. $apps = $table->all(array('order' => 'name'));
  49. if (isset($apps[$id])) {
  50. $this->_application = $apps[$id];
  51. } else if (!empty($apps)) {
  52. $this->_application = array_shift($apps);
  53. }
  54. return $this->_application;
  55. }
  56. // handle site
  57. if ($joomla->isSite()) {
  58. // get component params
  59. $params = $joomla->getParams();
  60. // create application from menu item params / request
  61. if ($item_id = $this->app->request->getInt('item_id')) {
  62. if ($item = $this->app->table->item->get($item_id)) {
  63. $this->_application = $item->getApplication();
  64. }
  65. } else if ($category_id = $this->app->request->getInt('category_id')) {
  66. if ($category = $this->app->table->category->get($category_id)) {
  67. $this->_application = $category->getApplication();
  68. }
  69. } else if ($submission_id = $this->app->request->getInt('submission_id')) {
  70. if ($submission = $this->app->table->submission->get($submission_id)) {
  71. $this->_application = $submission->getApplication();
  72. }
  73. } else if ($id = $this->app->request->getInt('app_id')) {
  74. $this->_application = $table->get($id);
  75. } else if ($id = $params->get('application')) {
  76. $this->_application = $table->get((int) $id);
  77. } else {
  78. // try to get application from default menu item
  79. $menu = $this->app->system->application->getMenu('site');
  80. $default = $menu->getDefault();
  81. if (isset($default->component) && $default->component == 'com_zoo') {
  82. if ($app_id = $menu->getParams($default->id)->get('application')) {
  83. $this->_application = $table->get((int) $app_id);
  84. }
  85. }
  86. }
  87. return $this->_application;
  88. }
  89. return null;
  90. }
  91. /**
  92. * Add help button to current toolbar to show help url in popup window.
  93. *
  94. * @param string $ref Help url
  95. * @since 2.0
  96. */
  97. public function toolbarHelp($ref = 'http://docs.yootheme.com/home/category/zoo-20') {
  98. JToolBar::getInstance('toolbar')->appendButton('Link', 'help', 'Help', $ref);
  99. }
  100. /**
  101. * Resize and cache image file.
  102. *
  103. * @param string $file
  104. * @param int $width
  105. * @param int $height
  106. *
  107. * @return string image path
  108. * @since 2.0
  109. */
  110. public function resizeImage($file, $width, $height) {
  111. // init vars
  112. $width = (int) $width;
  113. $height = (int) $height;
  114. $file_info = pathinfo($file);
  115. $thumbfile = $this->app->path->path('cache:').'/images/'.$file_info['filename'].'_'.md5($file.$width.$height).'.'.$file_info['extension'];
  116. $cache_time = 86400; // cache time 24h
  117. // check thumbnail directory
  118. if (!JFolder::exists(dirname($thumbfile))) {
  119. JFolder::create(dirname($thumbfile));
  120. }
  121. // create or re-cache thumbnail
  122. if ($this->app->imagethumbnail->check() && (!is_file($thumbfile) || ($cache_time > 0 && time() > (filemtime($thumbfile) + $cache_time)))) {
  123. $thumbnail = $this->app->imagethumbnail->create($file);
  124. if ($width > 0 && $height > 0) {
  125. $thumbnail->setSize($width, $height);
  126. $thumbnail->save($thumbfile);
  127. } else if ($width > 0 && $height == 0) {
  128. $thumbnail->sizeWidth($width);
  129. $thumbnail->save($thumbfile);
  130. } else if ($width == 0 && $height > 0) {
  131. $thumbnail->sizeHeight($height);
  132. $thumbnail->save($thumbfile);
  133. } else {
  134. if (JFile::exists($file)) {
  135. JFile::copy($file, $thumbfile);
  136. }
  137. }
  138. $this->putIndexFile(dirname($thumbfile));
  139. }
  140. if (is_file($thumbfile)) {
  141. return $thumbfile;
  142. }
  143. return $file;
  144. }
  145. /**
  146. * Trigger joomla content plugins on given text.
  147. *
  148. * @param string $text
  149. * @param array $params
  150. * @param string $context
  151. *
  152. * @return string The text after the plugins are applied to it
  153. * @since 2.0
  154. */
  155. public function triggerContentPlugins($text, $params = array(), $context = 'com_zoo') {
  156. // import joomla content plugins
  157. JPluginHelper::importPlugin('content');
  158. $registry = new JRegistry('');
  159. $registry->loadArray($params);
  160. $dispatcher = JDispatcher::getInstance();
  161. $article = JTable::getInstance('content');
  162. $article->text = $text;
  163. // disable loadmodule plugin on feed view
  164. if ($this->app->document->getType() == 'feed') {
  165. $plugin = JPluginHelper::getPlugin('content', 'loadmodule');
  166. if ($this->app->parameter->create($plugin->params)->get('enabled', 1)) {
  167. // expression to search for
  168. $regex = '/{loadposition\s*.*?}/i';
  169. $article->text = preg_replace($regex, '', $article->text);
  170. }
  171. }
  172. if ($this->app->joomla->isVersion('1.5')) {
  173. $dispatcher->trigger('onPrepareContent', array(&$article, &$registry, 0));
  174. } else {
  175. $dispatcher->trigger('onContentPrepare', array($context, &$article, &$registry, 0));
  176. }
  177. return $article->text;
  178. }
  179. /**
  180. * Returns user group objects.
  181. *
  182. * @return array
  183. * @since 2.0
  184. */
  185. public function getGroups() {
  186. if (!isset($this->_groups)) {
  187. if ($this->app->joomla->isVersion('1.5')) {
  188. $this->_groups = $this->app->database->queryObjectList("SELECT id, name FROM #__groups", "id");
  189. } else {
  190. $this->_groups = $this->app->database->queryObjectList("SELECT id, title AS name FROM #__viewlevels", "id");
  191. }
  192. }
  193. return $this->_groups;
  194. }
  195. /**
  196. * Return user group object.
  197. *
  198. * @param string $id
  199. *
  200. * @return object group
  201. * @since 2.0
  202. */
  203. public function getGroup($id) {
  204. $groups = $this->getGroups();
  205. return isset($groups[$id]) ? $groups[$id] : (object) array('id' => '', 'name' => '');
  206. }
  207. /**
  208. * Returns current ZOO version.
  209. *
  210. * @return string version
  211. * @since 2.0
  212. */
  213. public function version() {
  214. if (empty($this->_version) and $xml = simplexml_load_file($this->app->path->path('component.admin:zoo.xml')) and ((string) $xml->name == 'ZOO' || (string) $xml->name == 'com_zoo')) {
  215. $this->_version = (string) current($xml->xpath('//version'));
  216. }
  217. return $this->_version;
  218. }
  219. /**
  220. * Build page title from Joomla configuration.
  221. *
  222. * @param string $title
  223. *
  224. * @return string title
  225. * @since 2.0
  226. */
  227. public function buildPageTitle($title) {
  228. $dir = $this->app->system->application->getCfg('sitename_pagetitles', 0);
  229. if ($dir == 1) {
  230. return JText::sprintf('JPAGETITLE', $this->app->system->application->getCfg('sitename'), $title);
  231. } else if ($dir == 2) {
  232. return JText::sprintf('JPAGETITLE', $title, $this->app->system->application->getCfg('sitename'));
  233. }
  234. return $title;
  235. }
  236. /**
  237. * Puts an index.html into given directory
  238. *
  239. * @param string $dir
  240. * @since 2.0
  241. */
  242. public function putIndexFile($dir) {
  243. $dir = rtrim($dir, "\\/");
  244. if (!JFile::exists($dir.'/index.html')) {
  245. $buffer = '<!DOCTYPE html><title></title>';
  246. JFile::write($dir.'/index.html', $buffer);
  247. }
  248. }
  249. /**
  250. * Gets the application groups
  251. *
  252. * @return array groups
  253. * @since 2.0
  254. *
  255. * @deprecated 2.5.11 use ApplicationHelper::groups()
  256. */
  257. public function getApplicationGroups() {
  258. return $this->app->application->groups();
  259. }
  260. /**
  261. * Gets the application layouts
  262. *
  263. * @param Application $application The application object
  264. * @param string $type_id The id of the type
  265. * @param string $layout_type The type of the layout to fetch. Default: all
  266. *
  267. * @return array layouts
  268. * @since 2.0
  269. *
  270. * @deprecated 2.5.11 use TypeHelper::layouts()
  271. */
  272. public function getLayouts($application, $type_id, $layout_type = '') {
  273. return $this->app->type->layouts($application->getType($type_id), $layout_type);
  274. }
  275. }