]> git.mxchange.org Git - friendica.git/blob - src/Repository/ProfileField.php
Add permission set lazy loading to Profile Field model
[friendica.git] / src / Repository / ProfileField.php
1 <?php
2
3 namespace Friendica\Repository;
4
5 use Friendica\BaseModel;
6 use Friendica\BaseRepository;
7 use Friendica\Collection;
8 use Friendica\Database\Database;
9 use Friendica\Model;
10 use Friendica\Util\DateTimeFormat;
11 use Psr\Log\LoggerInterface;
12
13 class ProfileField extends BaseRepository
14 {
15         protected static $table_name = 'profile_field';
16
17         protected static $model_class = Model\ProfileField::class;
18
19         protected static $collection_class = Collection\ProfileFields::class;
20
21         /** @var PermissionSet */
22         private $permissionSet;
23
24         public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet)
25         {
26                 parent::__construct($dba, $logger);
27
28                 $this->permissionSet = $permissionSet;
29         }
30
31         /**
32          * @param array $data
33          * @return Model\ProfileField
34          */
35         protected function create(array $data)
36         {
37                 return new Model\ProfileField($this->dba, $this->logger, $this->permissionSet, $data);
38         }
39
40         /**
41          * @param array $condition
42          * @return Model\ProfileField
43          * @throws \Friendica\Network\HTTPException\NotFoundException
44          */
45         public function selectFirst(array $condition)
46         {
47                 return parent::selectFirst($condition);
48         }
49
50         /**
51          * @param array $condition
52          * @param array $params
53          * @return Collection\ProfileFields
54          * @throws \Exception
55          */
56         public function select(array $condition = [], array $params = [])
57         {
58                 return parent::select($condition, $params);
59         }
60
61         /**
62          * @param array $condition
63          * @param array $params
64          * @param int|null $max_id
65          * @param int|null $since_id
66          * @param int $limit
67          * @return Collection\ProfileFields
68          * @throws \Exception
69          */
70         public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
71         {
72                 return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
73         }
74
75         /**
76          * @param int $uid Field owner user Id
77          * @return Collection\ProfileFields
78          * @throws \Exception
79          */
80         public function selectByUserId(int $uid)
81         {
82                 return $this->select(
83                         ['uid' => $uid],
84                         ['order' => ['order']]
85                 );
86         }
87
88         /**
89          * Retrieve all custom profile field a given contact is able to access to, including public profile fields.
90          *
91          * @param int $cid Private contact id, must be owned by $uid
92          * @param int $uid Field owner user id
93          * @return Collection\ProfileFields
94          * @throws \Exception
95          */
96         public function selectByContactId(int $cid, int $uid)
97         {
98                 $permissionSets = $this->permissionSet->selectByContactId($cid, $uid);
99
100                 $psids = $permissionSets->column('id');
101
102                 // Includes public custom fields
103                 $psids[] = 0;
104
105                 return $this->select(
106                         ['uid' => $uid, 'psid' => $psids],
107                         ['order' => ['order']]
108                 );
109         }
110
111         /**
112          * @param array $fields
113          * @return Model\ProfileField|bool
114          * @throws \Exception
115          */
116         public function insert(array $fields)
117         {
118                 $fields['created'] = DateTimeFormat::utcNow();
119                 $fields['edited']  = DateTimeFormat::utcNow();
120
121                 return parent::insert($fields);
122         }
123
124         /**
125          * @param Model\ProfileField $model
126          * @return bool
127          * @throws \Exception
128          */
129         public function update(BaseModel $model)
130         {
131                 $model->edited = DateTimeFormat::utcNow();
132
133                 return parent::update($model);
134         }
135 }