123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
-
- namespace Muzich\CoreBundle\Searcher;
-
- use Doctrine\Bundle\DoctrineBundle\Registry;
- use Doctrine\ORM\NoResultException;
-
- class ElementSearcher extends Searcher implements SearcherInterface
- {
-
- /**
- * Constante définissant si la recherche porte sur le réseau public
- * ou sur le réseau personel de l'utilisateur.
- */
- const NETWORK_PUBLIC = 'network_public';
- const NETWORK_PERSONAL = 'network_personal';
-
- /**
- * Réseau sur lequel porte la recherche
- *
- * @var string
- */
- protected $network = self::NETWORK_PUBLIC;
-
- /**
- * Liste des tag_ids utilisés lors de la recherche.
- *
- * @var array
- */
- protected $tags = Array();
-
- /**
- * Nombre limite de résultats retournés.
- *
- * @var int
- */
- protected $count = 20;
-
- /**
- * Id de l'user si on limite la recherche a un utilisateur
- *
- * @var int
- */
- protected $user_id = null;
-
- /**
- * Id du groupe si on limite la recherche a un groupe
- *
- * @var int
- */
- protected $group_id = null;
-
- /**
- * Est-ce les favoris qui sont recherché
- *
- * @var boolean
- */
- protected $favorite = false;
-
- /**
- * Si id_limit est renseigné c'est que l'on veut trouver les elements
- * plus vieux (ont utilise l'id comme référence) que l'id_limi passé.
- * EDIT: Ou les éléments plus récents si $searchnew est a vrai
- *
- * @var type int
- */
- protected $id_limit = null;
-
- /**
- * Si searchnew est a vrai, c'est que l'on recherche les nouveau éléments
- * depuis id_limit.
- *
- * @var type boolean
- */
- protected $searchnew = false;
-
- /**
- * Ce tableaux peut conteni des ids. Si ces ids sont renseignés tout les
- * autres paramétres ne sont plus pris en compte.
- * Ces ids servent a filtrer directement la liste d'élément a afficher.
- *
- * @var array
- */
- protected $ids;
-
- /**
- * On stocke la dedans le bouton a afficher dans le gestionnaire de filtres
- * correspondant aux ids filtrés. La valeur doit correspondre a une constante
- * de l'Entité metier Event.
- *
- * @var string
- */
- protected $ids_display;
-
- /**
- * Ce booléen permet de savoir si la recherche de tag est stricte.
- * Si elle est stricte chaque tag choisis devrons être attaché au partage
- * pour qu'il soit pris en compte.
- *
- * @var type boolean
- */
- protected $tag_strict = false;
-
- /**
- * A renseigné pour une recherche portant sur les nom
- *
- * @var string
- */
- protected $string = null;
-
- /**
- * Pour la recherche de partage qui demande des tags.
- *
- * @var boolean
- */
- protected $need_tags = false;
-
- /**
- * @see SearcherInterface
- * @param array $params
- */
- public function init($params)
- {
- // Control des parametres transmis.
- // $this->checkParams($params, array(
- // 'tags' => "Muzich\CoreBundle\Searcher\ElementSearch::init(): \$params: Au moins un tag est nécéssaire"
- // ));
-
- // Mise a jour des attributs
- $this->setAttributes($params);
-
- }
-
- /**
- * @see SearcherInterface
- * @param array $params
- */
- public function update($params)
- {
- // Mise a jour des attributs
- $this->setAttributes($params);
- }
-
- /**
- * @see SearcherInterface
- *
- * @return array
- */
- public function getParams($tags_string = false)
- {
- return array(
- 'network' => $this->getNetwork(),
- 'tags' => $this->getTags($tags_string),
- 'count' => $this->getCount(),
- 'user_id' => $this->getUserId(),
- 'group_id' => $this->getGroupId(),
- 'favorite' => $this->isFavorite(),
- 'ids' => $this->getIds(),
- 'ids_display' => $this->getIdsDisplay(),
- 'tag_strict' => $this->getTagStrict(),
- 'need_tags' => $this->getNeedTags(),
- 'string' => $this->getString()
- );
- }
-
- public function getNetwork()
- {
- return $this->network;
- }
-
- public function isNetworkPublic()
- {
- if ($this->network == self::NETWORK_PUBLIC)
- {
- return true;
- }
- return false;
- }
-
- public function isNetworkPersonal()
- {
- if ($this->network == self::NETWORK_PERSONAL)
- {
- return true;
- }
- return false;
- }
-
- public function setNoTags()
- {
- $this->tags = array();
- }
-
- public function getTags($tags_string = false)
- {
- if (!$tags_string)
- {
- return $this->tags;
- }
-
- $ids = array();
- foreach ($this->tags as $id => $name)
- {
- $ids[] = $id;
- }
- return json_encode($ids);
- }
-
- public function getCount()
- {
- return $this->count;
- }
-
- public function getUserId()
- {
- return $this->user_id;
- }
-
- public function getIdLimit()
- {
- return $this->id_limit;
- }
-
- public function getGroupId()
- {
- return $this->group_id;
- }
-
- public function isFavorite()
- {
- return $this->favorite;
- }
-
- public function setIds($ids)
- {
- $this->ids = $ids;
- }
-
- public function getIds()
- {
- return $this->ids;
- }
-
- public function hasIds()
- {
- if (count($this->ids))
- {
- return true;
- }
- return false;
- }
-
- public function setIdsDisplay($display)
- {
- $this->ids_display = $display;
- }
-
- public function getIdsDisplay()
- {
- return $this->ids_display;
- }
-
- public function setTagStrict($strict)
- {
- $this->tag_strict = $strict;
- }
-
- public function getTagStrict()
- {
- return $this->tag_strict;
- }
-
- public function getNeedTags()
- {
- return ($this->need_tags);
- }
-
- public function setString($string)
- {
- $this->string = $string;
- }
-
- public function getString()
- {
- return $this->string;
- }
-
- /**
- * Construction de l'objet Query
- *
- * @param Registry $doctrine
- * @param int $user_id
- * @param string $exec_type
- *
- * @return collection
- */
- protected function constructQueryObject(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
- {
- $this->setQuery($doctrine
- ->getRepository('MuzichCoreBundle:Element')
- ->findBySearch($this, $user_id, $exec_type, $params))
- ;
- }
-
- /**
- * Retourne l'objet Query
- *
- * @param Registry $doctrine
- * @param int $user_id
- * @param string $exec_type
- *
- * @return collection
- */
- public function getQuery(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
- {
- $this->constructQueryObject($doctrine, $user_id, $exec_type, $params);
- return $this->query;
- }
-
- /**
- * Retourne les elements correspondant a la recherche
- * user_id: Identifiant de celui qui navigue
- *
- * @param Registry $doctrine
- * @param int $user_id
- * @param string $exec_type Type d'execution
- *
- * @return collection
- */
- public function getElements(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
- {
- $query = $this->getQuery($doctrine, $user_id, $exec_type, $params);
-
- switch ($exec_type)
- {
- case 'execute':
- return $query->execute();
- break;
-
- case 'count':
- return count($query->getArrayResult());
- break;
-
- case 'single':
- try
- {
- return $query->getSingleResult();
- }
- catch (NoResultException $exc)
- {
- return null;
- }
-
- break;
-
- default :
- throw new \Exception('Mode de récupération des Elements non supporté.');
- break;
- }
- }
-
- public function isSearchingNew()
- {
- return $this->searchnew;
- }
-
- public function isNeedTags()
- {
- if ($this->need_tags)
- {
- return true;
- }
- return false;
- }
-
- public function setNetwork($network)
- {
- if (!in_array($network, array(self::NETWORK_PERSONAL, self::NETWORK_PUBLIC)))
- {
- throw new \Exception("Wrong network set");
- }
- $this->network = $network;
- }
-
- }
|