Completed
Push — 3.0 ( 1cd324...cc19ac )
by Olivier
02:59
created

UserColumn::alter_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
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\Block\ManageBlock;
13
14
use ICanBoogie\ActiveRecord\Model;
15
use ICanBoogie\ActiveRecord\Query;
16
use ICanBoogie\ActiveRecord\RecordNotFound;
17
use Icybee\Block\ManageBlock;
18
use Icybee\Block\ManageBlock\Column;
19
use Icybee\Block\ManageBlock\FilterDecorator;
20
use Icybee\Modules\Users\User;
21
22
/**
23
 * Representation of a _user_ column.
24
 *
25
 * This column can be used to represent the users associated to records.
26
 */
27
class UserColumn extends Column
28
{
29
	/**
30
	 * @var string
31
	 */
32
	private $prefix;
33
34
	/**
35
	 * The users associated with the records, indexed by their identifier.
36
	 *
37
	 * @var User[]
38
	 */
39
	private $user_cache;
40
41
	/**
42
	 * The names of the users associated with the records, indexed by their identifier.
43
	 *
44
	 * @var array
45
	 *
46
	 * @see resolved_user_names()
47
	 */
48
	private $resolved_user_names;
49
50
	/**
51
	 * Initializes the {@link $resolved_user_names} property.
52
	 *
53
	 * @inheritdoc
54
	 */
55
	public function __construct(ManageBlock $manager, $id, array $options = [])
56
	{
57
		parent::__construct($manager, $id, $options + [
58
59
			'title' => 'User',
60
			'orderable' => true
61
62
		]);
63
64
		$this->prefix = 'user_' . uniqid() . '_';
65
//		$this->resolved_user_names = $this->resolve_user_names($manager->model);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
	}
67
68
	private function resolve_user_names(Model $model)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
69
	{
70
		$query = $model->select("DISTINCT `{$this->id}`");
71
72
		if ($model->has_scope('own'))
73
		{
74
			$query = $query->own;
75
		}
76
77
		if ($model->has_scope('similar_site'))
78
		{
79
			$query = $query->similar_site;
80
		}
81
82
		$users_keys = $query->all(\PDO::FETCH_COLUMN);
83
84
		if (count($users_keys) < 2)
85
		{
86
			return null;
87
		}
88
89
		return \ICanBoogie\app()
90
			->models['users']
91
			->select('uid, IF((firstname != "" AND lastname != ""), CONCAT_WS(" ", firstname, lastname), username) name')
92
			->filter_by_uid($users_keys)
93
			->order('name')
94
			->pairs;
95
	}
96
97
	/**
98
	 * @inheritdoc
99
	 */
100
	public function alter_query(Query $query)
101
	{
102
		$prefix = $this->prefix;
103
104
		$user_query = $this->app
105
			->models['users']
106
			->select("uid, firstname AS {$prefix}firstname, lastname AS {$prefix}lastname");
107
108
		return $query->join($user_query, [ 'on' => 'uid', 'mode' => 'LEFT', 'as' => $prefix ]);
109
	}
110
111
	public function alter_query_with_value(Query $query, $value)
112
	{
113
		$query = parent::alter_query_with_value($query, $value);
114
115
		if ($value)
116
		{
117
			$query->and([ $this->id => $value ]);
118
		}
119
120
		return $query;
121
	}
122
123
	public function alter_query_with_order(Query $query, $order_direction)
124
	{
125
		return $query->order("{$this->prefix}firstname " . ($order_direction < 0 ? 'DESC' : 'ASC'));
126
	}
127
128
	/**
129
	 * Includes the users associated with the records.
130
	 *
131
	 * @param User[] $records
132
	 *
133
	 * @inheritdoc
134
	 */
135
	public function __alter_records(array &$records)
136
	{
137
		parent::alter_records($records);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (alter_records() instead of __alter_records()). Are you sure this is correct? If so, you might want to change this to $this->alter_records().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
138
		$keys = [];
139
140
		foreach ($records as $record)
141
		{
142
			$keys[] = $record->{ $this->id };
143
		}
144
145
		if ($keys)
146
		{
147
			$keys = array_unique($keys, SORT_NUMERIC);
148
149
			try
150
			{
151
				$this->user_cache = \ICanBoogie\app()->models['users']->find($keys);
152
			}
153
			catch (RecordNotFound $e)
154
			{
155
				$this->user_cache = $e->records;
156
			}
157
		}
158
	}
159
160
	public function get_options()
161
	{
162
		if (!$this->resolved_user_names)
163
		{
164
			return null;
165
		}
166
167
		$options = [];
168
169
		foreach ($this->resolved_user_names as $uid => $name)
170
		{
171
			$options["?uid=" . urlencode($uid)] = $name;
172
		}
173
174
		return $options;
175
	}
176
177
	/**
178
	 * @param User $record
179
	 *
180
	 * @inheritdoc
181
	 */
182
	public function render_cell($record)
183
	{
184
		$uid = $record->{ $this->id };
185
		$user = $uid ? $this->user_cache[$uid] : null;
186
187
//		var_dump($record); exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
189
		if (!$user)
190
		{
191
			return <<<EOT
192
<div class="alert alert-danger">Undefined user: {$uid}</div>
193
EOT;
194
		}
195
196
		return new FilterDecorator($record, $this->id, $this->is_filtering, $user ? $user->name : '');
197
	}
198
}
199