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

/administrator/components/com_zoo/classes/exporter/k2.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 224 lines | 130 code | 36 blank | 58 comment | 13 complexity | be00cf7dbc835d74e26b64f39cca210d 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. * Exporter for K2 items and categories
  10. *
  11. * @package Component.Classes.Exporters
  12. */
  13. class AppExporterK2 extends AppExporter {
  14. /**
  15. * Class Constructor
  16. */
  17. public function __construct() {
  18. parent::__construct();
  19. $this->_name = 'K2';
  20. }
  21. /**
  22. * If K2 is installed on the system
  23. *
  24. * @return boolean If K2 is installed on the system
  25. *
  26. * @since 2.0
  27. */
  28. public function isEnabled() {
  29. $path_to_xml = JPATH_ADMINISTRATOR . '/components/com_k2/manifest.xml';
  30. if (JFile::exists($path_to_xml) && ($data = JApplicationHelper::parseXMLInstallFile($path_to_xml))) {
  31. return (version_compare($data['version'], '2.1') >= 0);
  32. }
  33. return false;
  34. }
  35. /**
  36. * Do the real export of items and categories
  37. *
  38. * @return string The JSON dump of the items and categories
  39. *
  40. * @since 2.0
  41. */
  42. public function export() {
  43. $db = $this->app->database;
  44. // get k2 categories
  45. $query = "SELECT a.*, b.name AS extra_field_group_name "
  46. ." FROM #__k2_categories AS a"
  47. ." LEFT JOIN #__k2_extra_fields_groups AS b ON b.id = a.extraFieldsGroup";
  48. $categories = $db->queryObjectList($query, 'id');
  49. // sanatize category aliases
  50. $aliases = array();
  51. foreach ($categories as $category) {
  52. $i = 2;
  53. $alias = $this->app->string->sluggify($category->alias);
  54. while (in_array($alias, $aliases)) {
  55. $alias = $category->alias . '-' . $i++;
  56. }
  57. $category->alias = $alias;
  58. // remember used aliases to ensure unique aliases
  59. $aliases[] = $category->alias;
  60. }
  61. // export categories
  62. foreach ($categories as $category) {
  63. // assign attributes
  64. $data = array();
  65. foreach ($this->category_attributes as $attribute) {
  66. if (isset($category->$attribute)) {
  67. $data[$attribute] = $category->$attribute;
  68. }
  69. }
  70. // sanatize parent
  71. if ($category->parent && isset($categories[$category->parent])) {
  72. $data['parent'] = $categories[$category->parent]->alias;
  73. }
  74. // add category
  75. if ($category->image) {
  76. $data['content']['image'] = '/media/k2/categories/'.$category->image;
  77. }
  78. $this->_addCategory($category->name, $category->alias, $data);
  79. }
  80. // get k2 items
  81. $query = "SELECT * FROM #__k2_items";
  82. $items = $db->queryObjectList($query, 'id');
  83. // get k2 extra fields
  84. $query = "SELECT * FROM #__k2_extra_fields";
  85. $extra_fields = $db->queryObjectList($query, 'id');
  86. // get k2 tags
  87. $query = "SELECT a.itemID, b.name"
  88. ." FROM #__k2_tags_xref as a"
  89. ." JOIN #__k2_tags AS b ON a.tagID = b.id";
  90. $tag_result = $db->queryObjectList($query);
  91. $tags = array();
  92. foreach ($tag_result as $tag) {
  93. $tags[$tag->itemID][] = $tag->name;
  94. }
  95. // sanatize item aliases
  96. $aliases = array();
  97. foreach ($items as $item) {
  98. $i = 2;
  99. $alias = $this->app->string->sluggify($item->alias);
  100. while (in_array($alias, $aliases)) {
  101. $alias = $item->alias . '-' . $i++;
  102. }
  103. $item->alias = $alias;
  104. // remember used aliases to ensure unique aliases
  105. $aliases[] = $item->alias;
  106. }
  107. // export items
  108. foreach ($items as $item) {
  109. if (!$item->trash) {
  110. if (!$type = $categories[$item->catid]->extra_field_group_name) {
  111. $type = JText::_('K2-Unassigned');
  112. }
  113. $this->_addItem($item, $extra_fields, $categories, $tags, $type);
  114. }
  115. }
  116. return parent::export();
  117. }
  118. /**
  119. * Add an item to the list of items to export
  120. *
  121. * @param object $item The item to export
  122. * @param array $extra_fields The extra fields for this item
  123. * @param array $categories The categories for this item
  124. * @param array $tags The item tags
  125. * @param string $group The item group
  126. *
  127. * @return AppExporterK2 $this for chaining support
  128. */
  129. protected function _addItem($item, $extra_fields, $categories = array(), $tags = array(), $group = 'default') {
  130. $data = array();
  131. foreach ($this->item_attributes as $attribute) {
  132. if (isset($item->$attribute)) {
  133. $data[$attribute] = $item->$attribute;
  134. }
  135. }
  136. // add author
  137. $data['author'] = $this->app->user->get($item->created_by)->username;
  138. // add state
  139. $data['state'] = $item->published;
  140. // add category
  141. $data['categories'][] = $categories[$item->catid]->alias;
  142. // add tags
  143. $data['tags'] = isset($tags[$item->id]) ? $tags[$item->id] : array();
  144. // add item content
  145. $i = 0;
  146. $data['elements'][$i]['type'] = 'textarea';
  147. $data['elements'][$i]['name'] = 'content';
  148. $data['elements'][$i++]['data'] = array(array('value' => $item->introtext), array('value' => $item->fulltext));
  149. $data['elements'][$i]['type'] = 'image';
  150. $data['elements'][$i]['name'] = 'image';
  151. $data['elements'][$i++]['data'] = array('file' => 'media/k2/items/src/'.md5("Image".$item->id).'.jpg');
  152. // add extra fields
  153. if (isset($item->extra_fields)) {
  154. foreach (json_decode($item->extra_fields) as $element) {
  155. $extrafield = $extra_fields[$element->id];
  156. switch ($extrafield->type) {
  157. case 'textfield':
  158. $data['elements'][$i]['type'] = 'text';
  159. $data['elements'][$i]['name'] = $extrafield->name;
  160. $data['elements'][$i++]['data'] = array(array('value' => $element->value));
  161. break;
  162. case 'textarea':
  163. $data['elements'][$i]['type'] = 'textarea';
  164. $data['elements'][$i]['name'] = $extrafield->name;
  165. $data['elements'][$i++]['data'] = array(array('value' => $element->value));
  166. break;
  167. case 'select':
  168. case 'multipleSelect':
  169. $data['elements'][$i]['type'] = 'select';
  170. $data['elements'][$i]['name'] = $extrafield->name;
  171. $data['elements'][$i++]['data'] = array('option' => $element->value);
  172. break;
  173. case 'radio':
  174. $data['elements'][$i]['type'] = 'radio';
  175. $data['elements'][$i]['name'] = $extrafield->name;
  176. $data['elements'][$i++]['data'] = array('option' => $element->value);
  177. break;
  178. case 'link':
  179. $data['elements'][$i]['type'] = 'link';
  180. $data['elements'][$i]['name'] = $extrafield->name;
  181. $data['elements'][$i++]['data'] = array(array('text' => $element->value[0], 'value' => $element->value[1]));
  182. break;
  183. }
  184. }
  185. }
  186. parent::_addItem($item->title, $item->alias, $group, $data);
  187. }
  188. }