1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Users;
13
14 use ICanBoogie\ActiveRecord\Query;
15 use ICanBoogie\Event;
16 use ICanBoogie\Exception;
17 use ICanBoogie\HTTP\HTTPError;
18 use ICanBoogie\HTTP\NotFound;
19
20 class ViewProvider extends \Icybee\Modules\Views\ActiveRecordProvider
21 {
22 /**
23 * @throws a HTTPException with code 404 if no record matching the conditions could be found
24 * and the view is of type "view".
25 *
26 * @throws a HTTPException with code 401 if the record is offline and user don't have access
27 * permission and the view is of type "view".
28 *
29 * @see BriskView.Provider::__invoke()
30 */
31 public function __invoke()
32 {
33 global $core;
34
35 $rc = parent::__invoke();
36
37 if ($rc instanceof User)
38 {
39 if (!$rc)
40 {
41 throw new NotFound('The requested record was not found.');
42 }
43
44 if (!$rc->is_activated)
45 {
46 if (!$core->user->has_permission(\ICanBoogie\Module::PERMISSION_ACCESS, $rc->constructor))
47 {
48 throw new HTTPError('The requested record requires authentication.', 401);
49 }
50
51 // $rc->username .= ' ✎';
52 }
53
54 /*
55 $page = isset($core->request->context->page) ? $core->request->context->page : null;
56
57 if ($page)
58 {
59 $page->title = $rc->title;
60
61 if ($this->view->type == 'view')
62 {
63 $page->node = $rc;
64 }
65 }
66 */
67 }
68
69 return $rc;
70 }
71
72 /**
73 * Returns the conditions unaltered.
74 *
75 * @see Icybee\Views.Provider::alter_conditions()
76 */
77 protected function alter_conditions(array $conditions)
78 {
79 return $conditions;
80 }
81
82 /**
83 * Alters the query to search for records from the same constructor, a similar site and a
84 * similar language.
85 *
86 * The method also alters the query if the `nid` or `slug` conditions are defined.
87 *
88 * Finaly if the return type is RETURN_MANY the query is altered to search for online nodes
89 * only.
90 *
91 * @see BriskView.ActiveRecordProvider::alter_query()
92 */
93 protected function alter_query(Query $query, array $conditions)
94 {
95 static $mapping = [ 'uid', 'constructor', 'username', 'language' ];
96
97 foreach ($mapping as $property)
98 {
99 if (!isset($conditions[$property]))
100 {
101 continue;
102 }
103
104 $filter = 'filter_by_' . $property;
105
106 $query->$filter($conditions[$property]);
107 }
108
109 if ($this->returns == self::RETURNS_MANY)
110 {
111 $query->filter_by_is_activated(true);
112 }
113
114 return parent::alter_query($query, $conditions)->order('created_at DESC');
115 }
116
117 /**
118 * Returns the rendering context unaltered.
119 *
120 * @see Icybee\Views.Provider::alter_context()
121 */
122 protected function alter_context(\BlueTihi\Context $context, Query $query, array $conditions)
123 {
124 return $context;
125 }
126
127 /**
128 * @return ActiveRecord|array[ActiveRecord]|null If the view's type is "view" the method returns an
129 * ActiveRecord, or null if no record matching the conditions could be found, otherwise the
130 * method returns an array of ActiveRecord.
131 *
132 * @see BriskView.ActiveRecordProvider::extract_result()
133 */
134 protected function extract_result(Query $query)
135 {
136 if ($this->returns == self::RETURNS_ONE)
137 {
138 return $query->one;
139 }
140
141 return parent::extract_result($query);
142 }
143 }