PageRenderTime 43ms CodeModel.GetById 8ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

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