|
1
|
|
|
<?php namespace XoopsModules\Smartobject\Form\Elements; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Contains the SmartObjectControl class |
|
5
|
|
|
* |
|
6
|
|
|
* @license GNU |
|
7
|
|
|
* @author marcan <[email protected]> |
|
8
|
|
|
* @link http://smartfactory.ca The SmartFactory |
|
9
|
|
|
* @package SmartObject |
|
10
|
|
|
* @subpackage SmartObjectForm |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
use XoopsModules\Smartobject; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class SmartFormParentCategoryElement |
|
17
|
|
|
* @package XoopsModules\Smartobject\Form\Elements |
|
18
|
|
|
*/ |
|
19
|
|
|
class SmartFormParentCategoryElement extends \XoopsFormSelect |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* SmartFormParentcategoryElement constructor. |
|
23
|
|
|
* @param string $object |
|
24
|
|
|
* @param string $key |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($object, $key) |
|
27
|
|
|
{ |
|
28
|
|
|
$addNoParent = isset($object->controls[$key]['addNoParent']) ? $object->controls[$key]['addNoParent'] : true; |
|
29
|
|
|
$criteria = new \CriteriaCompo(); |
|
30
|
|
|
$criteria->setSort('weight, name'); |
|
31
|
|
|
$categoryHandler = xoops_getModuleHandler('category', $object->handler->_moduleName); |
|
32
|
|
|
$categories = $categoryHandler->getObjects($criteria); |
|
33
|
|
|
|
|
34
|
|
|
require_once XOOPS_ROOT_PATH . '/class/tree.php'; |
|
35
|
|
|
$mytree = new \XoopsObjectTree($categories, 'categoryid', 'parentid'); |
|
36
|
|
|
parent::__construct($object->vars[$key]['form_caption'], $key, $object->getVar($key, 'e')); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$ret = []; |
|
39
|
|
|
$options = $this->getOptionArray($mytree, 'name', 0, '', $ret); |
|
40
|
|
|
if ($addNoParent) { |
|
41
|
|
|
$newOptions = ['0' => '----']; |
|
42
|
|
|
foreach ($options as $k => $v) { |
|
43
|
|
|
$newOptions[$k] = $v; |
|
44
|
|
|
} |
|
45
|
|
|
$options = $newOptions; |
|
46
|
|
|
} |
|
47
|
|
|
$this->addOptionArray($options); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get options for a category select with hierarchy (recursive) |
|
52
|
|
|
* |
|
53
|
|
|
* @param \XoopsObjectTree $tree |
|
54
|
|
|
* @param string $fieldName |
|
55
|
|
|
* @param int $key |
|
56
|
|
|
* @param string $prefix_curr |
|
57
|
|
|
* @param array $ret |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getOptionArray($tree, $fieldName, $key, $prefix_curr = '', &$ret) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($key > 0) { |
|
64
|
|
|
$value = $tree->tree[$key]['obj']->getVar($tree->_myId); |
|
|
|
|
|
|
65
|
|
|
$ret[$key] = $prefix_curr . $tree->tree[$key]['obj']->getVar($fieldName); |
|
66
|
|
|
$prefix_curr .= '-'; |
|
67
|
|
|
} |
|
68
|
|
|
if (isset($tree->tree[$key]['child']) && !empty($tree->tree[$key]['child'])) { |
|
69
|
|
|
foreach ($tree->tree[$key]['child'] as $childkey) { |
|
70
|
|
|
$this->getOptionArray($tree, $fieldName, $childkey, $prefix_curr, $ret); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $ret; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.