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

https://gitlab.com/vnsoftdev/amms · PHP · 225 lines · 131 code · 36 blank · 58 comment · 12 complexity · d7f1fc027efc3b21e4c0c63d00f01f8c MD5 · raw file

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