3 * FusionForge Effort Unit Factory
5 * Copyright 2017, Stéphane-Eymeric Bredthauer - TrivialDev
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once $gfcommon.'include/FFError.class.php';
24 require_once $gfcommon.'tracker/EffortUnitSet.class.php';
25 require_once $gfcommon.'tracker/EffortUnit.class.php';
27 class EffortUnitFactory extends FFError {
30 * @var EffortUnitSet $EffortUnitSet Effort Unit Set of the factory.
32 private $EffortUnitSet;
35 * @var array $Units array of EffortUnit object.
40 * @var array $UnitsData array of EffortUnits data.
45 * EffortUnitFactory - EffortUnitFactory object constructor
47 * @param EffortUnitSet $EffortUnitSet The Effort Unit Set object to which this EffortUnitFactory is associated.
49 function __construct(&$EffortUnitSet) {
50 parent::__construct();
51 if (!$EffortUnitSet || !is_object($EffortUnitSet)) {
52 $this->setError('EffortUnitFactory: No Valid EffortUnitSet Object');
55 if ($EffortUnitSet->isError()) {
56 $this->setError('EffortUnitFactory: '.$EffortUnitSet->getErrorMessage());
59 $this->EffortUnitSet =& $EffortUnitSet;
63 * getUnitsArr - return a array of Units data.
65 * @return array The array of Units data.
67 function getUnitsData() {
68 if ($this->UnitsData) {
69 return $this->UnitsData;
71 $this->UnitsData = array ();
72 $res = db_query_params ('SELECT * FROM effort_unit WHERE unit_set_id=$1 AND is_deleted=0 ORDER BY unit_position ASC',
73 array ($this->EffortUnitSet->getID()));
75 $this->setError(db_error());
78 while ($arr = db_fetch_array($res)) {
79 $this->UnitsData[] = $arr;
81 return $this->UnitsData;
85 * getUnits - return an array of Effort Unit objects.
87 * @return array The array of Effort Unit objects.
93 $this->Units = array ();
94 $unitsArr = $this->getUnitsData();
95 if (!$unitsArr && $this->isError()) {
96 $this->setError($this->getErrorMessage());
99 foreach ($unitsArr as $unitArr) {
100 $unit = new EffortUnit($this->EffortUnitSet, $unitArr['unit_id'], $unitArr);
101 if (!$unit && $unit->isError()) {
102 $this->setError($unit->getErrorMessage());
105 $this->Units[] = $unit;
112 * getUnitsArr - return an associative array of Units name & id.
114 * @return array The associative array of Units name & id.
116 function getUnitsArr() {
118 $unitsArr = $this->getUnitsData();
119 if (!$unitsArr && $this->isError()) {
120 $this->setError($this->getErrorMessage());
123 foreach ($unitsArr as $unitArr) {
124 $result[$unitArr['unit_id']] = $unitArr['unit_name'];
130 * getBaseUnit - return the base EffortUnit object.
132 * @return EffortUnit the base EffortUnit object.
134 function getBaseUnit() {
135 $units = $this->getUnits();
136 foreach($units as $unit) {
137 if ($unit->isBaseUnit()) {
145 * getUnitByName - get a EffortUnit object by name.
147 * @param string $name the name of the EffortUnit
148 * @return EffortUnit the base EffortUnit object.
150 function getUnitByName($name) {
151 $units = $this->getUnits();
152 foreach($units as $unit) {
153 if ($unit->getName()==$name) {
161 * encodedToValue - return the value of an effort expressed in encoded unit.
163 * @param string $encoded encoded effort
164 * @return integer the value of an effort expressed in encoded unit.
166 function encodedToValue($encoded){
168 if (preg_match('/^(\d+)U(\d+)$/',$encoded,$matches)) {
169 $unit = new EffortUnit($this->EffortUnitSet, $matches[2]);
170 $value = intval(intval($matches[1])/$unit->getConversionFactorForBaseUnit());
176 * encodedToValueInBaseUnit - return the value of an effort expressed in base unit.
178 * @param string $encoded encoded effort
179 * @return integer the value of an effort expressed in base unit.
181 function encodedToValueInBaseUnit($encoded){
183 if (preg_match('/^(\d+)U(\d+)$/',$encoded,$matches)) {
184 $value = intval(intval($matches[1]));
190 * encodedToUnitId - return the unit id of an encoded effort.
192 * @param string $encoded encoded effort
193 * @return integer the unit id of an encoded effort.
195 function encodedToUnitId($encoded) {
196 if (preg_match('/^(\d+)U(\d+)$/',$encoded,$matches)) {
197 $unitId = intval($matches[2]);
199 $units = $this->getUnitsArr();
201 $unitId = key($units);
207 * encodedToUnitName - return the unit name of an encoded effort.
209 * @param string $encoded encoded effort
210 * @return string the unit name of an encoded effort.
212 function encodedToUnitName($encoded) {
213 $units = $this->getUnitsArr();
214 if (preg_match('/^(\d+)U(\d+)$/',$encoded,$matches)) {
215 $unitName = $units[$matches[2]];
217 $unitName = reset($units);
223 * encodedToString - return the value and the unit name of an encoded effort.
225 * @param string $encoded encoded effort
226 * @return string the value and the unit name of an encoded effort.
228 function encodedToString($encoded) {
229 if (preg_match('/^(\d+)U(\d+)$/',$encoded,$matches)) {
230 $unit = new EffortUnit($this->EffortUnitSet, $matches[2]);
231 $string = intval(intval($matches[1])/$unit->getConversionFactorForBaseUnit()).' '.$unit->getName();
233 $units = $this->getUnitsArr();
234 $string = '0 '.reset($units);