1 | <?php |
||
2 | |||
3 | namespace Elgg\Entity; |
||
4 | |||
5 | /** |
||
6 | * Adds methods to save profile data to an ElggEntity. |
||
7 | * Data is stored in Annotations (and Metadata for BC reasons) |
||
8 | * |
||
9 | * @since 3.1 |
||
10 | */ |
||
11 | trait ProfileData { |
||
12 | |||
13 | /** |
||
14 | * Store profile data |
||
15 | * |
||
16 | * @param string $profile_field_name profile field name |
||
17 | * @param mixed $value profile data |
||
18 | * @param int $access_id access of the profile data |
||
19 | * |
||
20 | * @return void |
||
21 | */ |
||
22 | 14 | public function setProfileData(string $profile_field_name, $value, int $access_id = ACCESS_PRIVATE) { |
|
23 | // remove old values |
||
24 | 14 | $this->deleteProfileData($profile_field_name); |
|
25 | |||
26 | 14 | if (is_null($value) || $value === '') { |
|
27 | // don't try to store empty values |
||
28 | 4 | return; |
|
29 | } |
||
30 | |||
31 | // store new value(s) |
||
32 | 12 | if (!is_array($value)) { |
|
33 | 6 | $value = [$value]; |
|
34 | } |
||
35 | |||
36 | 12 | foreach ($value as $v) { |
|
37 | 12 | $this->annotate("profile:{$profile_field_name}", $v, $access_id, $this->guid); |
|
38 | } |
||
39 | |||
40 | // for BC, keep storing fields in MD, but we'll read annotations only |
||
41 | 12 | $this->$profile_field_name = $value; |
|
42 | 12 | } |
|
43 | |||
44 | /** |
||
45 | * Get profile data |
||
46 | * |
||
47 | * @param string $profile_field_name profile field name |
||
48 | * |
||
49 | * @return null|mixed null if no profile data was found |
||
50 | */ |
||
51 | 20 | public function getProfileData(string $profile_field_name) { |
|
52 | 20 | $annotations = $this->getAnnotations([ |
|
53 | 20 | 'annotation_name' => "profile:{$profile_field_name}", |
|
54 | 'limit' => false, |
||
55 | ]); |
||
56 | |||
57 | 20 | if (empty($annotations)) { |
|
58 | 15 | return null; |
|
59 | } |
||
60 | |||
61 | 12 | if (!is_array($annotations)) { |
|
62 | $annotations = [$annotations]; |
||
63 | } |
||
64 | |||
65 | 12 | $result = []; |
|
66 | 12 | foreach ($annotations as $annotation) { |
|
67 | 12 | if ($annotation instanceof \ElggAnnotation) { |
|
68 | 12 | $result[] = $annotation->value; |
|
69 | 12 | continue; |
|
70 | } |
||
71 | |||
72 | // non saved entity has annotation as pure value |
||
73 | $result[] = $annotation; |
||
74 | } |
||
75 | |||
76 | 12 | if (count($result) === 1) { |
|
77 | 6 | return $result[0]; |
|
78 | } |
||
79 | |||
80 | 6 | return $result; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Remove profile data |
||
85 | * |
||
86 | * @param string $profile_field_name the profile field name to remove |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | 14 | public function deleteProfileData(string $profile_field_name) { |
|
91 | 14 | $result = $this->deleteAnnotations("profile:{$profile_field_name}"); |
|
92 | 14 | $result &= $this->deleteMetadata($profile_field_name); |
|
93 | |||
94 | 14 | return $result; |
|
1 ignored issue
–
show
Bug
Best Practice
introduced
by
![]() |
|||
95 | } |
||
96 | } |
||
97 |