1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Tiki_Profile_InstallHandler_Category extends Tiki_Profile_InstallHandler
9{
10	private $name;
11	private $description = '';
12	private $parent = 0;
13	private $migrateparent = 0;
14	private $items = [];
15
16	function fetchData()
17	{
18		if ($this->name) {
19			return;
20		}
21
22		$data = $this->obj->getData();
23
24		if (array_key_exists('name', $data)) {
25			$this->name = $data['name'];
26		}
27		if (array_key_exists('description', $data)) {
28			$this->description = $data['description'];
29		}
30		if (array_key_exists('parent', $data)) {
31			$this->parent = $data['parent'];
32		}
33		if (array_key_exists('migrateparent', $data)) {
34			$this->migrateparent = $data['migrateparent'];
35		}
36		if (array_key_exists('items', $data) && is_array($data['items'])) {
37			foreach ($data['items'] as $pair) {
38				if (is_array($pair) && count($pair) == 2) {
39					$this->items[] = $pair;
40				}
41			}
42		}
43	}
44
45	function canInstall()
46	{
47		$this->fetchData();
48
49		if (empty($this->name)) {
50			return false;
51		}
52
53		return true;
54	}
55
56	function _install()
57	{
58		global $tikilib;
59		$this->fetchData();
60		$this->replaceReferences($this->name);
61		$this->replaceReferences($this->description);
62		$this->replaceReferences($this->parent);
63		$this->replaceReferences($this->migrateparent);
64		$this->replaceReferences($this->items);
65		$this->replaceReferences($this->tplGroupContainer);
66		$this->replaceReferences($this->tplGroupPattern);
67
68		$categlib = TikiLib::lib('categ');
69		if ($id = $categlib->exist_child_category($this->parent, $this->name)) {
70			$categlib->update_category($id, $this->name, $this->description, $this->parent, $this->tplGroupContainer, $this->tplGroupPattern);
71		} else {
72			$id = $categlib->add_category($this->parent, $this->name, $this->description, $this->tplGroupContainer, $this->tplGroupPattern);
73		}
74
75		if ($this->migrateparent && $from = $categlib->exist_child_category($this->migrateparent, $this->name)) {
76			$categlib->move_all_objects($from, $id);
77		}
78
79		foreach ($this->items as $item) {
80			list( $type, $object ) = $item;
81
82			$type = Tiki_Profile_Installer::convertType($type);
83			$object = Tiki_Profile_Installer::convertObject($type, $object);
84			$categlib->categorize_any($type, $object, $id);
85		}
86
87		return $id;
88	}
89
90	/**
91	 * Export categories
92	 *
93	 * @param Tiki_Profile_Writer $writer
94	 * @param int $categId
95	 * @param bool $deep
96	 * @param mixed $includeObjectCallback
97	 * @param bool $all
98	 * @return bool
99	 */
100	public static function export(Tiki_Profile_Writer $writer, $categId, $deep, $includeObjectCallback, $all = false)
101	{
102		$categlib = TikiLib::lib('categ');
103
104		if (isset($categId) && ! $all) {
105			$listCategories = [];
106			$listCategories[] = $categlib->get_category($categId);
107			$error = isset($listCategories[0]) ? false : true;
108		} else {
109			$listCategories = $categlib->getCategories();
110			$error = isset($listCategories[1]) ? false : true;
111		}
112
113		if ($error) {
114			return false;
115		}
116
117		foreach ($listCategories as $category) {
118			$categId = $category['categId'];
119
120			$items = [];
121			foreach ($categlib->get_category_objects($categId) as $row) {
122				if ($includeObjectCallback($row['type'], $row['itemId'])) {
123					$items[] = [$row['type'], $writer->getReference($row['type'], $row['itemId'])];
124				}
125			}
126
127			$data = [
128				'name' => $category['name'],
129			];
130
131			if (! empty($category['parentId'])) {
132				$data['parent'] = $writer->getReference('category', $category['parentId']);
133			}
134
135			if (! empty($items)) {
136				$data['items'] = $items;
137			}
138
139			$writer->addObject('category', $categId, $data);
140
141			if ($deep) {
142				$descendants = $categlib->get_category_descendants($categId);
143				array_shift($descendants);
144				foreach ($descendants as $children) {
145					self::export($writer, $children, $deep, $includeObjectCallback);
146				}
147			}
148		}
149
150		return true;
151	}
152
153	/**
154	 * Remove category
155	 *
156	 * @param string $category
157	 * @return bool
158	 */
159	function remove($category)
160	{
161		if (! empty($category)) {
162			$categlib = TikiLib::lib('categ');
163			$categoryId = $categlib->get_category_id($category);
164			if (! empty($categoryId) && $categlib->remove_category($categoryId)->numRows()) {
165				return true;
166			}
167		}
168		return false;
169	}
170
171	/**
172	 * Return category object data
173	 *
174	 * @return array
175	 */
176	public function getData()
177	{
178		return $this->obj->getData();
179	}
180
181	/**
182	 * Get current category data
183	 *
184	 * @param array $category
185	 * @return mixed
186	 */
187	public function getCurrentData($category)
188	{
189		$categoryName = ! empty($category['name']) ? $category['name'] : '';
190		if (! empty($categoryName)) {
191			$categlib = TikiLib::lib('categ');
192			$categId = $categlib->get_category_id($categoryName);
193			if (isset($categId)) {
194				$categData = $categlib->get_category($categId);
195				if (! empty($categData)) {
196					return $categData;
197				}
198			}
199		}
200		return false;
201	}
202}
203