|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
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 Zikula\UsersModule\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
15
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Zikula\Core\Controller\AbstractController; |
|
19
|
|
|
use Zikula\UsersModule\Constant as UsersConstant; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @Route("/livesearch") |
|
23
|
|
|
*/ |
|
24
|
|
|
class LiveSearchController extends AbstractController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Retrieves a list of users for a given search term (fragment). |
|
28
|
|
|
* |
|
29
|
|
|
* @Route("/getUsers", options={"expose"=true}) |
|
30
|
|
|
* @Method("GET") |
|
31
|
|
|
* |
|
32
|
|
|
* @param Request $request Current request instance |
|
33
|
|
|
* |
|
34
|
|
|
* @return JsonResponse |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getUsersAction(Request $request) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!$this->hasPermission('ZikulaUsersModule::LiveSearch', '::', ACCESS_EDIT)) { |
|
39
|
|
|
return true; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$fragment = $request->query->get('fragment', ''); |
|
43
|
|
|
$userRepository = $this->get('zikula_users_module.user_repository'); |
|
44
|
|
|
$limit = 50; |
|
45
|
|
|
$filter = [ |
|
46
|
|
|
'activated' => ['operator' => 'notIn', 'operand' => [ |
|
47
|
|
|
UsersConstant::ACTIVATED_PENDING_REG, |
|
48
|
|
|
UsersConstant::ACTIVATED_PENDING_DELETE |
|
49
|
|
|
]], |
|
50
|
|
|
'uname' => ['operator' => 'like', 'operand' => '%' . $fragment . '%'] |
|
51
|
|
|
]; |
|
52
|
|
|
$results = $userRepository->query($filter, ['uname' => 'asc'], $limit); |
|
53
|
|
|
|
|
54
|
|
|
// load avatar plugin |
|
55
|
|
|
// @todo fix this as part of https://github.com/zikula-modules/Profile/issues/80 |
|
56
|
|
|
include_once 'lib/legacy/viewplugins/function.useravatar.php'; |
|
57
|
|
|
$view = \Zikula_View::getInstance('ZikulaUsersModule', false); |
|
58
|
|
|
|
|
59
|
|
|
$resultItems = []; |
|
60
|
|
|
if (count($results) > 0) { |
|
61
|
|
|
foreach ($results as $result) { |
|
62
|
|
|
$resultItems[] = [ |
|
63
|
|
|
'uid' => $result->getUid(), |
|
64
|
|
|
'uname' => $result->getUname(), |
|
65
|
|
|
'avatar' => smarty_function_useravatar(['uid' => $result->getUid(), 'rating' => 'g'], $view) |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// return response |
|
71
|
|
|
return new JsonResponse($resultItems); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.