PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_roksprocket/lib/RokSprocket/Provider/Zoo.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 321 lines | 239 code | 31 blank | 51 comment | 35 complexity | c5f8fbe60205084ca1277c7e55a4fda9 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2013 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. class RokSprocket_Provider_Zoo extends RokSprocket_Provider_AbstarctJoomlaBasedProvider
  9. {
  10. /**
  11. * @return RokSprocket_ItemCollection
  12. */
  13. public function getItems()
  14. {
  15. if ($this->params->exists('zoo_application_type')) {
  16. $this->filters['zoo_application_type'][] = $this->params->get('zoo_application_type');
  17. }
  18. return parent::getItems();
  19. }
  20. /**
  21. * @static
  22. * @return bool
  23. */
  24. public static function isAvailable()
  25. {
  26. if (!class_exists('JFactory')) {
  27. return false;
  28. }
  29. $db = JFactory::getDbo();
  30. $query = $db->getQuery(true);
  31. $query->select('a.extension_id');
  32. $query->from('#__extensions AS a');
  33. $query->where('a.type = "component"');
  34. $query->where('a.element = "com_zoo"');
  35. $query->where('a.enabled = 1');
  36. $db->setQuery($query);
  37. if ($db->loadResult()) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. /**
  44. * @param array $filters
  45. * @param array $sort_filters
  46. */
  47. public function __construct($filters = array(), $sort_filters = array())
  48. {
  49. parent::__construct('zoo');
  50. $this->setFilterChoices($filters, $sort_filters);
  51. }
  52. /**
  53. * @param $raw_item
  54. * @param int $dborder
  55. *
  56. * @return \RokSprocket_Item
  57. */
  58. protected function convertRawToItem($raw_item, $dborder = 0)
  59. {
  60. $app_type = $this->params->get('zoo_application_type');
  61. //$textfield = $this->params->get('zoo_articletext_field', '');
  62. $item = new RokSprocket_Item();
  63. $item->setProvider($this->provider_name);
  64. $item->setId($raw_item->id);
  65. $item->setAlias(($raw_item->created_by_alias) ? $raw_item->created_by_alias : $raw_item->author_name);
  66. $item->setAuthor($raw_item->author_name);
  67. $item->setTitle($raw_item->title);
  68. $item->setDate($raw_item->created);
  69. $item->setPublished(($raw_item->published == 1) ? true : false);
  70. $item->setCategory($raw_item->category_title);
  71. $item->setHits($raw_item->hits);
  72. $item->setRating($raw_item->rating);
  73. $item->setMetaKey('');
  74. $item->setMetaDesc('');
  75. $item->setMetaData('');
  76. require_once(JPATH_ADMINISTRATOR . '/components/com_zoo/config.php');
  77. $app = App::getInstance('zoo');
  78. $applications = $app->application->getApplications();
  79. $application = $applications[$raw_item->application_id];
  80. $text_ids = array();
  81. $image_ids = array();
  82. $link_ids = array();
  83. $types = $application->getTypes();
  84. foreach ($types as $type) {
  85. $elements = $type->getElements();
  86. foreach ($elements as $element) {
  87. if ($element->config->type == 'image') {
  88. $image_ids[] = $element->identifier;
  89. }
  90. if ($element->config->type == 'link') {
  91. $link_ids[] = $element->identifier;
  92. }
  93. if ($element->config->type == 'textarea' || $element->config->type == 'text') {
  94. $text_ids[] = $element->identifier;
  95. }
  96. }
  97. }
  98. $els = json_decode($raw_item->elements);
  99. $texts = array();
  100. $images = array();
  101. $links = array();
  102. foreach ($els as $ident => $el) {
  103. if (in_array($ident, $image_ids)) {
  104. $image = new RokSprocket_Item_Image();
  105. $image->setSource($el->file);
  106. $image->setIdentifier('image_field_' . $ident);
  107. $image->setCaption((isset($el->title))?$el->title:'');
  108. $image->setAlttext((isset($el->title))?$el->title:'');
  109. $images[$image->getIdentifier()] = $image;
  110. if (isset($images['image_field_' . $ident]) && !$item->getPrimaryImage()) {
  111. $item->setPrimaryImage($image);
  112. }
  113. }
  114. if (in_array($ident, $link_ids)) {
  115. $link = new RokSprocket_Item_Link();
  116. $link->setUrl((isset($el->url))?$el->url:((isset($el->value))?$el->value:''));
  117. $link->setText('');
  118. $link->setIdentifier('link_field_' . $ident);
  119. $links[$link->getIdentifier()] = $link;
  120. if (isset($links['link_field_' . $ident]) && !$item->getPrimaryLink()) {
  121. $item->setPrimaryLink($link);
  122. }
  123. }
  124. if (in_array($ident, $text_ids) && is_object($el)) {
  125. foreach ($el as $val){
  126. $texts['text_field_' . $ident] = $val->value;
  127. }
  128. }
  129. else if (in_array($ident, $text_ids) && !is_object($el)){
  130. $texts['text_field_' . $ident] = $el->value;
  131. }
  132. }
  133. $item->setImages($images);
  134. $item->setLinks($links);
  135. $params = RokCommon_JSON::decode($raw_item->params);
  136. $desc = "metadata.description";
  137. $texts['text_field_metadesc'] = $params->$desc;
  138. $texts = $this->processPlugins($texts);
  139. $item->setTextFields($texts);
  140. $text = array_values($texts);
  141. $text = array_shift($text);
  142. $item->setText($text);
  143. $primary_link = new RokSprocket_Item_Link();
  144. $primary_link->setUrl(JRoute::_('index.php?option=com_zoo&task=item&item_id=' . $raw_item->id, true));
  145. $primary_link->getIdentifier('article_link');
  146. $item->setPrimaryLink($primary_link);
  147. $item->setCommentCount($raw_item->comment_count);
  148. $tags = (explode(',', $raw_item->tags)) ? explode(',', $raw_item->tags) : array();
  149. $item->setTags($tags);
  150. $item->setDbOrder($dborder);
  151. return $item;
  152. }
  153. /**
  154. * @param $id
  155. *
  156. * @param bool $raw return the raw object not the RokSprocket_Item
  157. *
  158. * @return stdClass|RokSprocket_Item
  159. * @throws RokSprocket_Exception
  160. */
  161. public function getArticleInfo($id, $raw = false)
  162. {
  163. /** @var $filer_processor RokCommon_Filter_IProcessor */
  164. $filer_processor = $this->getFilterProcessor();
  165. $filer_processor->process(array('id' => array($id)));
  166. $query = $filer_processor->getQuery();
  167. $db = JFactory::getDbo();
  168. $db->setQuery($query);
  169. $ret = $db->loadObject();
  170. if ($error = $db->getErrorMsg()) {
  171. throw new RokSprocket_Exception($error);
  172. }
  173. if ($raw) {
  174. $ret->preview = $this->_cleanPreview($ret->articletext);
  175. $ret->editUrl = $this->getArticleEditUrl($id);
  176. return $ret;
  177. } else {
  178. $item = $this->convertRawToItem($ret);
  179. $item->editUrl = $this->getArticleEditUrl($id);
  180. $item->preview = $this->_cleanPreview($item->getText());
  181. return $item;
  182. }
  183. }
  184. /**
  185. * @param $id
  186. *
  187. * @return string
  188. */
  189. protected function getArticleEditUrl($id)
  190. {
  191. return JURI::root(true) . '/administrator/index.php?option=com_zoo&controller=item&changeapp=1&task=edit&cid[]=' . $id;
  192. }
  193. /**
  194. * @return array the array of image type and label
  195. */
  196. public static function getImageTypes()
  197. {
  198. require_once(JPATH_ADMINISTRATOR . '/components/com_zoo/config.php');
  199. $app = App::getInstance('zoo');
  200. $applications = $app->application->getApplications();
  201. $list = array();
  202. foreach ($applications as $application) {
  203. $types = $application->getTypes();
  204. foreach ($types as $type) {
  205. $elements = $type->getElements();
  206. foreach ($elements as $element) {
  207. if ($element->config->type == 'image') {
  208. $key = 'image_field_' . $element->identifier;
  209. $list[$key] = array();
  210. $list[$key]['group'] = $application->id . '_' . $type->id;
  211. $list[$key]['display'] = $element->config->name;
  212. }
  213. }
  214. }
  215. }
  216. return $list;
  217. }
  218. /**
  219. * @return array the array of link types and label
  220. */
  221. public static function getLinkTypes()
  222. {
  223. require_once(JPATH_ADMINISTRATOR . '/components/com_zoo/config.php');
  224. $app = App::getInstance('zoo');
  225. $applications = $app->application->getApplications();
  226. $list = array();
  227. foreach ($applications as $application) {
  228. $types = $application->getTypes();
  229. foreach ($types as $type) {
  230. $elements = $type->getElements();
  231. foreach ($elements as $element) {
  232. if ($element->config->type == 'link') {
  233. $key = 'link_field_' . $element->identifier;
  234. $list[$key] = array();
  235. $list[$key]['group'] = $application->id . '_' . $type->id;
  236. $list[$key]['display'] = $element->config->name;
  237. }
  238. }
  239. }
  240. }
  241. return $list;
  242. }
  243. /**
  244. * @return array the array of link types and label
  245. */
  246. public static function getTextTypes()
  247. {
  248. require_once(JPATH_ADMINISTRATOR . '/components/com_zoo/config.php');
  249. $app = App::getInstance('zoo');
  250. $applications = $app->application->getApplications();
  251. $list = array();
  252. foreach ($applications as $application) {
  253. $types = $application->getTypes();
  254. foreach ($types as $type) {
  255. $elements = $type->getElements();
  256. foreach ($elements as $element) {
  257. if ($element->config->type == 'textarea' || $element->config->type == 'text') {
  258. $key = 'text_field_' . $element->identifier;
  259. $list[$key] = array();
  260. $list[$key]['group'] = $application->id . '_' . $type->id;
  261. $list[$key]['display'] = $element->config->name;
  262. }
  263. }
  264. }
  265. }
  266. $static = array(
  267. 'text_field_metadesc' => array('group' => null, 'display' => 'Meta Description Text'),
  268. );
  269. $list = array_merge($static, $list);
  270. return $list;
  271. }
  272. /**
  273. * @static
  274. * @return array
  275. */
  276. public static function getCCKGroups()
  277. {
  278. require_once(JPATH_ADMINISTRATOR . '/components/com_zoo/config.php');
  279. $app = App::getInstance('zoo');
  280. $applications = $app->application->getApplications();
  281. $list = array();
  282. foreach ($applications as $application) {
  283. $types = $application->getTypes();
  284. foreach ($types as $type) {
  285. $list[$application->id . '_' . $type->id] = $application->name . ' - ' . $type->name;
  286. }
  287. }
  288. return $list;
  289. }
  290. }