summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole3/backend/src/Opengnsys/ServerBundle/Repository/TraceRepository.php
blob: ddd2d7170e6d09abd8c4344ea7fb16a952b650ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

/*
 * This file is part of the Opengnsys Project package.
 *
 * Created by Miguel Angel de Vega Alcantara on 01/10/18. <miguelangel.devega@sic.uhu.es>
 * Copyright (c) 2015 Opengnsys. All rights reserved.
 *
 */

namespace Opengnsys\ServerBundle\Repository;

use Opengnsys\CoreBundle\Repository\BaseRepository;

/**
 * TraceRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TraceRepository extends BaseRepository
{
    public function searchStatus($clients , $ou){
        $qb = $this->createQueryBuilder('o');

        $qb->select('o.id, o.status');

        if($ou){
            $qb->andWhere("o.organizationalUnit = :organizationalUnit")->setParameter("organizationalUnit", $ou);
        }

        if($clients){
            $qb->andWhere("o.id in (:clients)")->setParameter("clients", $clients);
        }

        try {
            $objects = $qb->getQuery()->getScalarResult();
        } catch (NoResultException $e) {
            $message = sprintf('Unable to find an objects');
            throw new NotFoundHttpException($message, null, 0, $e);
        }
        return $objects;
    }

    public function findByObservable($term = "", $limit = null, $offset = null, $ordered = array(), $selects = array(), $searchs = array(), $matching = array())
    {
        $qb = $this->createQueryBuilder('o');

        if(count($selects) > 0){
            $qb = $this->createSelect($qb, $selects);
        }else{
            $qb->select("DISTINCT o.createdAt, o.updatedAt, o.notes, o.title, o.commandType, o.script, o.status, o.output, o.error, o.executedAt, o.finishedAt, o.id");
        }

        if($term != ""){
            if(count($searchs) > 0){
                $qb = $this->createSearch($qb, $term, $searchs);
            }else{
                $qb->andWhere("o.createdAt LIKE :term OR o.updatedAt LIKE :term OR o.notes LIKE :term OR o.title LIKE :term OR o.commandType LIKE :term OR o.script LIKE :term OR o.status LIKE :term OR o.output LIKE :term OR o.error LIKE :term OR o.executedAt LIKE :term OR o.finishedAt LIKE :term OR o.id LIKE :term ")->setParameter('term', '%' . $term . '%');
            }
        }

        $qb = $this->createMaching($qb, $matching);

        $qb = $this->createOrdered($qb, $ordered);

        if($limit != null){
            $qb->setMaxResults($limit);
        }

        if($offset){
            $qb->setFirstResult($offset);
        }

        $entities = $qb->getQuery()->getScalarResult();
        return $entities;
    }

    public function countFiltered($term = "", $searchs = array(), $matching = array())
    {
        $qb = $this->createQueryBuilder('o');

        $qb->select("count(DISTINCT o.id)");

        if($term != ""){
            if(count($searchs) > 0){
                $qb = $this->createSearch($qb, $term, $searchs);
            }else{
                $qb->andWhere("o.createdAt LIKE :term OR o.updatedAt LIKE :term OR o.notes LIKE :term OR o.title LIKE :term OR o.commandType LIKE :term OR o.script LIKE :term OR o.status LIKE :term OR o.output LIKE :term OR o.error LIKE :term OR o.executedAt LIKE :term OR o.finishedAt LIKE :term OR o.id LIKE :term ")->setParameter('term', '%' . $term . '%');
            }
        }

        $qb = $this->createMaching($qb, $matching);

        $count = $qb->getQuery()->getSingleScalarResult();
        return $count;
    }

    protected function createMaching($qb, $matching)
    {
        if(array_key_exists('finished', $matching) && $matching['finished'] != null){
            if($matching['finished']){
                $qb->andWhere("o.status is not null");
            }else{
                $qb->andWhere("o.status is null");
            }
            unset($matching['finished']);
        }

        if(array_key_exists("fromDate", $matching) && $matching['fromDate'] != null){
            $qb->andWhere("o.executedAt >= :fromDate")->setParameter("fromDate", $matching['fromDate']);
        }
        unset($matching['fromDate']);

        if(array_key_exists("toDate", $matching) && $matching['toDate'] != null){
            $qb->andWhere("o.executedAt <= :toDate")->setParameter("toDate", $matching['toDate']);
        }
        unset($matching['toDate']);

        $qb = parent::createMaching($qb, $matching);

        return $qb;
    }
}