1
|
|
|
<?php |
2
|
|
|
namespace samsoncms\api; |
3
|
|
|
|
4
|
|
|
use samsonframework\orm\Condition; |
5
|
|
|
use samsonframework\orm\QueryInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SamsonCMS additional field table entity class |
9
|
|
|
* @package samson\cms |
10
|
|
|
*/ |
11
|
|
|
class Field extends \samson\activerecord\field |
12
|
|
|
{ |
13
|
|
|
/** @var string Additional field value type */ |
14
|
|
|
public $type; |
15
|
|
|
|
16
|
|
|
/** @var string Additional field name */ |
17
|
|
|
public $Name; |
18
|
|
|
|
19
|
|
|
/** @var string Default field value */ |
20
|
|
|
public $Value; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Find additional field database record by Name. |
24
|
|
|
* This is generic method that should be used in nested classes to find its |
25
|
|
|
* records by some its primary key value. |
26
|
|
|
* |
27
|
|
|
* @param QueryInterface $query Query object instance |
28
|
|
|
* @param string $name Additional field name |
29
|
|
|
* @param self $return Variable to return found database record |
30
|
|
|
* @return bool|null|self Field instance or null if 3rd parameter not passed |
31
|
|
|
*/ |
32
|
|
|
public static function byName(QueryInterface $query, $name, self & $return = null) |
33
|
|
|
{ |
34
|
|
|
// Get field record by name column |
35
|
|
|
$return = static::oneByColumn($query, 'Name', $name); |
36
|
|
|
|
37
|
|
|
// If only one argument is passed - return null, otherwise bool |
38
|
|
|
return func_num_args() > 1 ? $return == null : $return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Find additional field database record by Name or ID. |
43
|
|
|
* This is generic method that should be used in nested classes to find its |
44
|
|
|
* records by some its primary key value. |
45
|
|
|
* |
46
|
|
|
* @param QueryInterface $query Query object instance |
47
|
|
|
* @param string $nameOrID Additional field name or identifier |
48
|
|
|
* @param self $return Variable to return found database record |
49
|
|
|
* @return bool|null|self Field instance or null if 3rd parameter not passed |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
// Create id or URL condition |
54
|
|
|
$idOrUrl = new Condition('OR'); |
55
|
|
|
$idOrUrl->add('FieldID', $nameOrID)->add('Name', $nameOrID); |
56
|
|
|
|
57
|
|
|
// Perform query |
58
|
|
|
$return = $query->entity(get_called_class())->where($idOrUrl)->first(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
// If only one argument is passed - return null, otherwise bool |
61
|
|
|
return func_num_args() > 1 ? $return == null : $return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* If this field has defined key=>value set. |
66
|
|
|
* |
67
|
|
|
* @return array|mixed Grouped collection of field key => value possible values or value for key passed. |
68
|
|
|
*/ |
69
|
|
|
public function options($key = null) |
70
|
|
|
{ |
71
|
|
|
$types = array(); |
72
|
|
|
// Convert possible field values to array |
73
|
|
|
foreach (explode(',', $this->Value) as $typeValue) { |
74
|
|
|
// Split view and value |
75
|
|
|
$typeValue = explode(':', $typeValue); |
76
|
|
|
|
77
|
|
|
// Store to key => value collection |
78
|
|
|
$types[$typeValue[0]] = $typeValue[1]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return isset($key) ? $types[$key] : $types; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @return string Get additional field value field name depending on its type */ |
85
|
|
|
public function valueFieldName() |
86
|
|
|
{ |
87
|
|
|
switch ($this->type) { |
88
|
|
|
case 7: |
89
|
|
|
return 'numeric_value'; |
90
|
|
|
break; |
|
|
|
|
91
|
|
|
case 6: |
92
|
|
|
return 'key_value'; |
93
|
|
|
break; |
|
|
|
|
94
|
|
|
default: |
95
|
|
|
return 'Value'; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.