1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Pedigree; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
You may not change or alter any portion of this comment or credits |
7
|
|
|
of supporting developers from this source code or any supporting source code |
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
/** |
15
|
|
|
* Pedigree module for XOOPS |
16
|
|
|
* |
17
|
|
|
* @package XoopsModules\Pedigree |
18
|
|
|
* @copyright {@link https://github.com/Xoops The XOOPS Project} |
19
|
|
|
* @license {@link https://www.fsf.org/copyleft/gpl.html GNU public license} |
20
|
|
|
* @since 1.3.1 |
21
|
|
|
* @author XOOPS Module Dev Team |
22
|
|
|
* @author ZySpec <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
use XoopsModules\Pedigree\{ |
25
|
|
|
Constants, |
|
|
|
|
26
|
|
|
Field, |
|
|
|
|
27
|
|
|
FieldsHandler, |
|
|
|
|
28
|
|
|
TreeHandler |
|
|
|
|
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Animal Class |
35
|
|
|
*/ |
36
|
|
|
class Animal |
37
|
|
|
{ |
38
|
|
|
protected $myTree = []; |
39
|
|
|
protected $fields = []; // array of ids for fields |
40
|
|
|
protected $configValues = []; // id indexed array of field info |
41
|
|
|
private $helper; // module Helper object |
42
|
|
|
private $treeHandler; // Pedigree Tree handler object |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Animal class constructor |
46
|
|
|
* |
47
|
|
|
* initializes the tree array |
48
|
|
|
* @param int|null $id |
49
|
|
|
*/ |
50
|
|
|
public function __construct(?int $id = null) |
51
|
|
|
{ |
52
|
|
|
$id = !empty($id) ? (int) $id : Constants::DEFAULT_TREE_ID; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Helper $this->helper |
56
|
|
|
* @var TreeHandler $this->treeHandler; |
57
|
|
|
*/ |
58
|
|
|
$this->helper = Helper::getInstance(); |
59
|
|
|
$this->treeHandler = $this->helper->getHandler('Tree'); |
60
|
|
|
$this->myTree = $this->treeHandler->get($id); |
61
|
|
|
/* |
|
|
|
|
62
|
|
|
$SQL = "SELECT * FROM " . $GLOBALS['xoopsDB']->prefix("pedigree_tree") . " WHERE id = {$id}"; |
63
|
|
|
$result = $GLOBALS['xoopsDB']->query($SQL); |
64
|
|
|
$row = $GLOBALS['xoopsDB']->fetchRow($result); |
65
|
|
|
$numfields = mysqli_num_fields($result); |
66
|
|
|
for ($i = 0; $i < $numfields; ++$i) { |
67
|
|
|
$key =$GLOBALS['xoopsDB']->getFieldName($result, $i); |
68
|
|
|
$this->$key = $row[$i]; |
69
|
|
|
} |
70
|
|
|
*/ |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get Ids of Fields |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getFieldsIds(): array |
79
|
|
|
{ |
80
|
|
|
/** @var FieldsHandler $fieldsHandler */ |
81
|
|
|
$fieldsHandler = $this->helper->getHandler('Fields'); |
82
|
|
|
$criteria = new \Criteria(''); |
83
|
|
|
$criteria->setSort('order'); |
84
|
|
|
$criteria->order = 'ASC'; |
85
|
|
|
//$this->fields = $fieldsHandler->getIds($criteria); //get all object IDs |
|
|
|
|
86
|
|
|
$this->configValues = $fieldsHandler->getAll($criteria, null, false, true); //get array of fields w/ id as array key |
87
|
|
|
$this->fields = array_keys($this->configValues); |
88
|
|
|
/* |
|
|
|
|
89
|
|
|
$SQL = "SELECT * FROM " . $GLOBALS['xoopsDB']->prefix("pedigree_fields") . " ORDER BY `order`"; |
90
|
|
|
$result = $GLOBALS['xoopsDB']->query($SQL); |
91
|
|
|
$fields = array(); |
92
|
|
|
while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
93
|
|
|
$fields[] = $row['id']; |
94
|
|
|
$configValues[] = $row; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
$this->configValues = isset($configValues) ? $configValues : ''; |
98
|
|
|
//print_r ($this->configValues); die(); |
99
|
|
|
*/ |
100
|
|
|
unset($fieldsHandler, $criteria); |
101
|
|
|
|
102
|
|
|
return $this->fields; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get indexed array of field configs |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getConfig(): array |
111
|
|
|
{ |
112
|
|
|
return $this->configValues; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get Fields Elements to display in a form |
117
|
|
|
* |
118
|
|
|
* example usage: $formElements = $animal->getFormFieldsElements(); |
119
|
|
|
* foreach ($formElements as $fElement) { |
120
|
|
|
* $form->addElement($fElement); |
121
|
|
|
* } |
122
|
|
|
* |
123
|
|
|
* @return array form element array to be added to form |
124
|
|
|
*/ |
125
|
|
|
public function getFormFieldsElements(): array |
126
|
|
|
{ |
127
|
|
|
$formElements = []; |
128
|
|
|
$this->fields = $this->getFieldsIds(); |
129
|
|
|
$fieldCount = count($this->fields); |
130
|
|
|
for ($i = 0; $i < $fieldCount; ++$i) { |
131
|
|
|
$userField = new Field($this->fields[$i], $this->getConfig()); |
132
|
|
|
if ($userField->isActive()) { |
133
|
|
|
$fieldType = $userField->getSetting('fieldtype'); |
134
|
|
|
$fieldObject = new $fieldType($userField, $this); |
135
|
|
|
$edditable[$i] = $fieldObject->editField(); |
136
|
|
|
$formElements[] = $edditable[$i]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return $formElements; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.