PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/foxbei/joomla15
PHP | 479 lines | 300 code | 71 blank | 108 comment | 37 complexity | f814f8fb2f8acab5d50c7ebdfc18ea90 MD5 | raw file
  1. <?php
  2. /**
  3. * @package ZOO Component
  4. * @file zoo.php
  5. * @version 2.3.7 March 2011
  6. * @author YOOtheme http://www.yootheme.com
  7. * @copyright Copyright (C) 2007 - 2011 YOOtheme GmbH
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die('Restricted access');
  12. /*
  13. Class: ZooHelper
  14. The general helper Class for zoo
  15. */
  16. class ZooHelper {
  17. /*
  18. Function: toolbarHelp
  19. Add help button to current toolbar to show help url in popup window.
  20. Parameters:
  21. $ref - Help url
  22. Returns:
  23. Void
  24. */
  25. public static function toolbarHelp($ref = 'http://docs.yootheme.com/home/category/zoo-20') {
  26. $toolbar = JToolBar::getInstance('toolbar');
  27. $toolbar->appendButton('ZooHelp', $ref);
  28. }
  29. /*
  30. Function: resizeImage
  31. Resize and cache image file.
  32. Returns:
  33. String - image path
  34. */
  35. public static function resizeImage($file, $width, $height) {
  36. // init vars
  37. $width = (int) $width;
  38. $height = (int) $height;
  39. $file_info = pathinfo($file);
  40. $thumbfile = JPATH_ROOT.'/cache/com_zoo/images/'. $file_info['filename'] . '_' . md5($file.$width.$height) . '.' . $file_info['extension'];
  41. $cache_time = 86400; // cache time 24h
  42. // check thumbnail directory
  43. if (!JFolder::exists(dirname($thumbfile))) {
  44. JFolder::create(dirname($thumbfile));
  45. }
  46. // create or re-cache thumbnail
  47. if (YImageThumbnail::check() && (!is_file($thumbfile) || ($cache_time > 0 && time() > (filemtime($thumbfile) + $cache_time)))) {
  48. $thumbnail = new YImageThumbnail($file);
  49. if ($width > 0 && $height > 0) {
  50. $thumbnail->setSize($width, $height);
  51. $thumbnail->save($thumbfile);
  52. } else if ($width > 0 && $height == 0) {
  53. $thumbnail->sizeWidth($width);
  54. $thumbnail->save($thumbfile);
  55. } else if ($width == 0 && $height > 0) {
  56. $thumbnail->sizeHeight($height);
  57. $thumbnail->save($thumbfile);
  58. } else {
  59. if (JFile::exists($file)) {
  60. JFile::copy($file, $thumbfile);
  61. }
  62. }
  63. }
  64. if (is_file($thumbfile)) {
  65. return $thumbfile;
  66. }
  67. return $file;
  68. }
  69. /*
  70. Function: triggerContentPlugins
  71. Trigger joomla content plugins on given text.
  72. Parameters:
  73. $text - Text
  74. Returns:
  75. String - Text
  76. */
  77. public static function triggerContentPlugins($text) {
  78. // import joomla content plugins
  79. JPluginHelper::importPlugin('content');
  80. $params = new JParameter('');
  81. $dispatcher = JDispatcher::getInstance();
  82. $article = JTable::getInstance('content');
  83. $article->text = $text;
  84. // disable loadmodule plugin on feed view
  85. if (JFactory::getDocument()->getType() == 'feed') {
  86. $plugin = JPluginHelper::getPlugin('content', 'loadmodule');
  87. $pluginParams = new JParameter($plugin->params);
  88. if ($pluginParams->get('enabled', 1)) {
  89. // expression to search for
  90. $regex = '/{loadposition\s*.*?}/i';
  91. $article->text = preg_replace($regex, '', $article->text);
  92. }
  93. }
  94. $dispatcher->trigger('onPrepareContent', array(&$article, &$params, 0));
  95. return $article->text;
  96. }
  97. /*
  98. Function: getGroups
  99. Returns user group objects.
  100. Returns:
  101. Array - groups
  102. */
  103. public static function getGroups() {
  104. $db = JFactory::getDBO();
  105. $query = "SELECT g.id, g.name FROM #__groups AS g";
  106. $db->setQuery($query);
  107. return $db->loadObjectList("id");
  108. }
  109. /*
  110. Function: getLayouts
  111. Returns layouts for a type of an app.
  112. Returns:
  113. Array - layouts
  114. */
  115. public static function getLayouts($application, $type_id, $layout_type = '') {
  116. $result = array();
  117. // get template
  118. if ($template = $application->getTemplate()) {
  119. // get renderer
  120. $renderer = new ItemRenderer();
  121. $renderer->addPath($template->getPath());
  122. $path = 'item';
  123. $prefix = 'item.';
  124. if ($renderer->pathExists($path.DIRECTORY_SEPARATOR.$type_id)) {
  125. $path .= DIRECTORY_SEPARATOR.$type_id;
  126. $prefix .= $type_id.'.';
  127. }
  128. foreach ($renderer->getLayouts($path) as $layout) {
  129. $metadata = $renderer->getLayoutMetaData($prefix.$layout);
  130. if (empty($layout_type) || ($metadata->get('type') == $layout_type)) {
  131. $result[$layout] = $metadata;
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. }
  138. /*
  139. Class: JHTMLZoo
  140. A class that contains zoo html functions
  141. */
  142. class JHTMLZoo {
  143. /*
  144. Function: calendar
  145. Get zoo datepicker.
  146. Returns:
  147. Returns zoo datepicker html string.
  148. */
  149. public static function calendar($value, $name, $id, $attribs = null) {
  150. if (!defined('ZOO_CALENDAR_SCRIPT_DECLARATION')) {
  151. define('ZOO_CALENDAR_SCRIPT_DECLARATION', true);
  152. JHTML::script('date.js', 'administrator/components/com_zoo/assets/js/');
  153. $translations = array(
  154. 'closeText' => 'Done',
  155. 'currentText' => 'Today',
  156. 'dayNames' => array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
  157. 'dayNamesMin' => array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'),
  158. 'dayNamesShort' => array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'),
  159. 'monthNames' => array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
  160. 'monthNamesShort' => array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
  161. 'prevText' => 'Prev',
  162. 'nextText' => 'Next',
  163. 'weekHeader' => 'Wk',
  164. 'appendText' => '(yyyy-mm-dd)'
  165. );
  166. foreach ($translations as $key => $translation) {
  167. $translations[$key] = is_array($translation) ? array_map(create_function('$text', 'return JText::_($text);'), $translation) : JText::_($translation);
  168. }
  169. $javascript = 'jQuery(function($) { $("body").Calendar({ translations: '.json_encode($translations).' }); });';
  170. JFactory::getDocument()->addScriptDeclaration($javascript);
  171. }
  172. if (is_array($attribs)) {
  173. $attribs = JArrayHelper::toString($attribs);
  174. }
  175. return '<input style="width: 110px" type="text" name="'.$name.'" id="'.$id.'" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" '.$attribs.' />'
  176. .'<img src="'.JURI::root(true).'/templates/system/images/calendar.png'.'" class="zoo-calendar" />';
  177. }
  178. /*
  179. Function: image
  180. Get image resource info.
  181. Returns:
  182. Array - Image info
  183. */
  184. public static function image($image, $width = null, $height = null) {
  185. $resized_image = ZooHelper::resizeImage(JPATH_ROOT.DS.$image, $width, $height);
  186. $inner_path = trim(str_replace('\\', '/', preg_replace('/^'.preg_quote(JPATH_ROOT, '/').'/i', '', $resized_image)), '/');
  187. $path = JPATH_ROOT.'/'.$inner_path;
  188. if (is_file($path) && $size = getimagesize($path)) {
  189. $info['path'] = $path;
  190. $info['src'] = JURI::root().$inner_path;
  191. $info['mime'] = $size['mime'];
  192. $info['width'] = $size[0];
  193. $info['height'] = $size[1];
  194. $info['width_height'] = sprintf('width="%d" height="%d"', $info['width'], $info['height']);
  195. return $info;
  196. }
  197. return null;
  198. }
  199. /*
  200. Function: categoryList
  201. Returns category select list html string.
  202. */
  203. public static function categoryList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false) {
  204. // set options
  205. if (is_array($options)) {
  206. reset($options);
  207. } else {
  208. $options = array($options);
  209. }
  210. // get category tree list
  211. $list = CategoryHelper::buildTreeList(0, $application->getCategoryTree(), array(), '-&nbsp;', '.&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;');
  212. // create options
  213. foreach ($list as $category) {
  214. $options[] = JHTML::_('select.option', $category->id, $category->treename);
  215. }
  216. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  217. }
  218. /*
  219. Function: layoutList
  220. Returns layout select list html string.
  221. */
  222. public static function layoutList($application, $type_id, $layout_type, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false) {
  223. // set options
  224. if (is_array($options)) {
  225. reset($options);
  226. } else {
  227. $options = array($options);
  228. }
  229. $layouts = ZooHelper::getLayouts($application, $type_id, $layout_type);
  230. foreach ($layouts as $layout => $metadata) {
  231. $options[] = JHTML::_('select.option', $layout, $metadata->get('name'));
  232. }
  233. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  234. // create options
  235. foreach ($application->getTemplates() as $template) {
  236. $metadata = $template->getMetadata();
  237. $options[] = JHTML::_('select.option', $template->name, $metadata['name']);
  238. }
  239. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  240. }
  241. /*
  242. Function: typeList
  243. Returns type select list html string.
  244. */
  245. public static function typeList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $filter = array()) {
  246. if (is_array($options)) {
  247. reset($options);
  248. } else {
  249. $options = array($options);
  250. }
  251. foreach (Zoo::getApplication()->getTypes() as $type) {
  252. if (empty($filter) || in_array($type->id, $filter)) {
  253. $options[] = JHTML::_('select.option', $type->id, JText::_($type->name));
  254. }
  255. }
  256. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  257. }
  258. /*
  259. Function: authorList
  260. Returns author select list html string.
  261. */
  262. public static function authorList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $show_registered_users = true) {
  263. $query = 'SELECT DISTINCT u.id AS value, u.name AS text'
  264. .' FROM #__users AS u'
  265. . ' WHERE u.block = 0'
  266. . ($show_registered_users ? '' : ' AND u.gid > 18');
  267. return self::queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  268. }
  269. /*
  270. Function: itemAuthorList
  271. Returns author select list html string.
  272. */
  273. public static function itemAuthorList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  274. $query = 'SELECT DISTINCT u.id AS value, u.name AS text'
  275. .' FROM '.ZOO_TABLE_ITEM.' AS i'
  276. .' JOIN #__users AS u ON i.created_by = u.id';
  277. return self::queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  278. }
  279. /*
  280. Function: itemList
  281. Returns item select list html string.
  282. */
  283. public static function itemList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  284. $query = 'SELECT DISTINCT c.item_id as value, a.name as text'
  285. .' FROM '.ZOO_TABLE_COMMENT.' AS c'
  286. .' LEFT JOIN '.ZOO_TABLE_ITEM. ' AS a ON c.item_id = a.id'
  287. .' WHERE a.application_id = ' . $application->id
  288. .' ORDER BY a.name';
  289. if (is_array($options)) {
  290. reset($options);
  291. } else {
  292. $options = array($options);
  293. }
  294. $db = YDatabase::getInstance();
  295. $rows = $db->queryAssocList($query);
  296. foreach ($rows as $row) {
  297. $options[] = JHTML::_('select.option', $row['value'], $row['text']);
  298. }
  299. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  300. }
  301. /*
  302. Function: accessLevel
  303. Returns user access select list.
  304. */
  305. public static function accessLevel($selected = null, $exclude = array()) {
  306. if (is_array($exclude)) {
  307. reset($exclude);
  308. } else {
  309. $exclude = array($exclude);
  310. }
  311. $query = 'SELECT id AS value, name AS text'
  312. . ' FROM #__groups'
  313. . ' ORDER BY id';
  314. $db = YDatabase::getInstance();
  315. $groups = $db->queryAssocList($query);
  316. foreach($exclude as $key) {
  317. unset($groups[$key]);
  318. }
  319. return JHTML::_('select.genericlist', $groups, 'access', 'class="inputbox"', 'value', 'text', $selected, '', 1);
  320. }
  321. /*
  322. Function: itemAuthorList
  323. Returns author select list html string.
  324. */
  325. public static function commentAuthorList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  326. $query = "SELECT DISTINCT c.author AS value, c.author AS text"
  327. ." FROM ".ZOO_TABLE_COMMENT." AS c"
  328. .' LEFT JOIN '.ZOO_TABLE_ITEM. ' AS a ON c.item_id = a.id'
  329. ." WHERE c.author <> ''"
  330. ." AND a.application_id = " . $application->id
  331. ." ORDER BY c.author";
  332. return self::queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  333. }
  334. /*
  335. Function: queryList
  336. Returns select list html string.
  337. */
  338. public static function queryList($query, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  339. if (is_array($options)) {
  340. reset($options);
  341. } else {
  342. $options = array($options);
  343. }
  344. $db = YDatabase::getInstance();
  345. $list = $db->queryObjectList($query);
  346. $options = array_merge($options, $list);
  347. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  348. }
  349. /*
  350. Function: arrayList
  351. Returns select list html string.
  352. */
  353. public static function arrayList($array, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  354. if (is_array($options)) {
  355. reset($options);
  356. } else {
  357. $options = array($options);
  358. }
  359. $options = array_merge($options, JHTMLZoo::listOptions($array));
  360. return JHTML::_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  361. }
  362. /*
  363. Function: selectOptions
  364. Returns select option as JHTML compatible array.
  365. */
  366. public static function listOptions($array, $value = 'value', $text = 'text') {
  367. $options = array();
  368. if (is_array($array)) {
  369. foreach ($array as $val => $txt) {
  370. $options[] = JHTML::_('select.option', strval($val), $txt, $value, $text);
  371. }
  372. }
  373. return $options;
  374. }
  375. }