ElementSearcher.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace Muzich\CoreBundle\Searcher;
  3. use Doctrine\Bundle\DoctrineBundle\Registry;
  4. use Doctrine\ORM\NoResultException;
  5. class ElementSearcher extends Searcher implements SearcherInterface
  6. {
  7. /**
  8. * Constante définissant si la recherche porte sur le réseau public
  9. * ou sur le réseau personel de l'utilisateur.
  10. */
  11. const NETWORK_PUBLIC = 'network_public';
  12. const NETWORK_PERSONAL = 'network_personal';
  13. /**
  14. * Réseau sur lequel porte la recherche
  15. *
  16. * @var string
  17. */
  18. protected $network = self::NETWORK_PUBLIC;
  19. /**
  20. * Liste des tag_ids utilisés lors de la recherche.
  21. *
  22. * @var array
  23. */
  24. protected $tags = Array();
  25. /**
  26. * Nombre limite de résultats retournés.
  27. *
  28. * @var int
  29. */
  30. protected $count = 20;
  31. /**
  32. * Id de l'user si on limite la recherche a un utilisateur
  33. *
  34. * @var int
  35. */
  36. protected $user_id = null;
  37. /**
  38. * Id du groupe si on limite la recherche a un groupe
  39. *
  40. * @var int
  41. */
  42. protected $group_id = null;
  43. /**
  44. * Est-ce les favoris qui sont recherché
  45. *
  46. * @var boolean
  47. */
  48. protected $favorite = false;
  49. /**
  50. * Si id_limit est renseigné c'est que l'on veut trouver les elements
  51. * plus vieux (ont utilise l'id comme référence) que l'id_limi passé.
  52. * EDIT: Ou les éléments plus récents si $searchnew est a vrai
  53. *
  54. * @var type int
  55. */
  56. protected $id_limit = null;
  57. /**
  58. * Si searchnew est a vrai, c'est que l'on recherche les nouveau éléments
  59. * depuis id_limit.
  60. *
  61. * @var type boolean
  62. */
  63. protected $searchnew = false;
  64. /**
  65. * Ce tableaux peut conteni des ids. Si ces ids sont renseignés tout les
  66. * autres paramétres ne sont plus pris en compte.
  67. * Ces ids servent a filtrer directement la liste d'élément a afficher.
  68. *
  69. * @var array
  70. */
  71. protected $ids;
  72. /**
  73. * On stocke la dedans le bouton a afficher dans le gestionnaire de filtres
  74. * correspondant aux ids filtrés. La valeur doit correspondre a une constante
  75. * de l'Entité metier Event.
  76. *
  77. * @var string
  78. */
  79. protected $ids_display;
  80. /**
  81. * Ce booléen permet de savoir si la recherche de tag est stricte.
  82. * Si elle est stricte chaque tag choisis devrons être attaché au partage
  83. * pour qu'il soit pris en compte.
  84. *
  85. * @var type boolean
  86. */
  87. protected $tag_strict = false;
  88. /**
  89. * A renseigné pour une recherche portant sur les nom
  90. *
  91. * @var string
  92. */
  93. protected $string = null;
  94. /**
  95. * Pour la recherche de partage qui demande des tags.
  96. *
  97. * @var boolean
  98. */
  99. protected $need_tags = false;
  100. /**
  101. * @see SearcherInterface
  102. * @param array $params
  103. */
  104. public function init($params)
  105. {
  106. // Control des parametres transmis.
  107. // $this->checkParams($params, array(
  108. // 'tags' => "Muzich\CoreBundle\Searcher\ElementSearch::init(): \$params: Au moins un tag est nécéssaire"
  109. // ));
  110. // Mise a jour des attributs
  111. $this->setAttributes($params);
  112. }
  113. /**
  114. * @see SearcherInterface
  115. * @param array $params
  116. */
  117. public function update($params)
  118. {
  119. // Mise a jour des attributs
  120. $this->setAttributes($params);
  121. }
  122. /**
  123. * @see SearcherInterface
  124. *
  125. * @return array
  126. */
  127. public function getParams($tags_string = false)
  128. {
  129. return array(
  130. 'network' => $this->getNetwork(),
  131. 'tags' => $this->getTags($tags_string),
  132. 'count' => $this->getCount(),
  133. 'user_id' => $this->getUserId(),
  134. 'group_id' => $this->getGroupId(),
  135. 'favorite' => $this->isFavorite(),
  136. 'ids' => $this->getIds(),
  137. 'ids_display' => $this->getIdsDisplay(),
  138. 'tag_strict' => $this->getTagStrict(),
  139. 'need_tags' => $this->getNeedTags(),
  140. 'string' => $this->getString()
  141. );
  142. }
  143. public function getNetwork()
  144. {
  145. return $this->network;
  146. }
  147. public function isNetworkPublic()
  148. {
  149. if ($this->network == self::NETWORK_PUBLIC)
  150. {
  151. return true;
  152. }
  153. return false;
  154. }
  155. public function isNetworkPersonal()
  156. {
  157. if ($this->network == self::NETWORK_PERSONAL)
  158. {
  159. return true;
  160. }
  161. return false;
  162. }
  163. public function setNoTags()
  164. {
  165. $this->tags = array();
  166. }
  167. public function getTags($tags_string = false)
  168. {
  169. if (!$tags_string)
  170. {
  171. return $this->tags;
  172. }
  173. $ids = array();
  174. foreach ($this->tags as $id => $name)
  175. {
  176. $ids[] = $id;
  177. }
  178. return json_encode($ids);
  179. }
  180. public function getCount()
  181. {
  182. return $this->count;
  183. }
  184. public function getUserId()
  185. {
  186. return $this->user_id;
  187. }
  188. public function getIdLimit()
  189. {
  190. return $this->id_limit;
  191. }
  192. public function getGroupId()
  193. {
  194. return $this->group_id;
  195. }
  196. public function isFavorite()
  197. {
  198. return $this->favorite;
  199. }
  200. public function setIds($ids)
  201. {
  202. $this->ids = $ids;
  203. }
  204. public function getIds()
  205. {
  206. return $this->ids;
  207. }
  208. public function hasIds()
  209. {
  210. if (count($this->ids))
  211. {
  212. return true;
  213. }
  214. return false;
  215. }
  216. public function setIdsDisplay($display)
  217. {
  218. $this->ids_display = $display;
  219. }
  220. public function getIdsDisplay()
  221. {
  222. return $this->ids_display;
  223. }
  224. public function setTagStrict($strict)
  225. {
  226. $this->tag_strict = $strict;
  227. }
  228. public function getTagStrict()
  229. {
  230. return $this->tag_strict;
  231. }
  232. public function getNeedTags()
  233. {
  234. return ($this->need_tags);
  235. }
  236. public function setString($string)
  237. {
  238. $this->string = $string;
  239. }
  240. public function getString()
  241. {
  242. return $this->string;
  243. }
  244. /**
  245. * Construction de l'objet Query
  246. *
  247. * @param Registry $doctrine
  248. * @param int $user_id
  249. * @param string $exec_type
  250. *
  251. * @return collection
  252. */
  253. protected function constructQueryObject(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  254. {
  255. $this->setQuery($doctrine
  256. ->getRepository('MuzichCoreBundle:Element')
  257. ->findBySearch($this, $user_id, $exec_type, $params))
  258. ;
  259. }
  260. /**
  261. * Retourne l'objet Query
  262. *
  263. * @param Registry $doctrine
  264. * @param int $user_id
  265. * @param string $exec_type
  266. *
  267. * @return collection
  268. */
  269. public function getQuery(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  270. {
  271. $this->constructQueryObject($doctrine, $user_id, $exec_type, $params);
  272. return $this->query;
  273. }
  274. /**
  275. * Retourne les elements correspondant a la recherche
  276. * user_id: Identifiant de celui qui navigue
  277. *
  278. * @param Registry $doctrine
  279. * @param int $user_id
  280. * @param string $exec_type Type d'execution
  281. *
  282. * @return collection
  283. */
  284. public function getElements(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  285. {
  286. $query = $this->getQuery($doctrine, $user_id, $exec_type, $params);
  287. switch ($exec_type)
  288. {
  289. case 'execute':
  290. return $query->execute();
  291. break;
  292. case 'count':
  293. return count($query->getArrayResult());
  294. break;
  295. case 'single':
  296. try
  297. {
  298. return $query->getSingleResult();
  299. }
  300. catch (NoResultException $exc)
  301. {
  302. return null;
  303. }
  304. break;
  305. default :
  306. throw new \Exception('Mode de récupération des Elements non supporté.');
  307. break;
  308. }
  309. }
  310. public function isSearchingNew()
  311. {
  312. return $this->searchnew;
  313. }
  314. public function isNeedTags()
  315. {
  316. if ($this->need_tags)
  317. {
  318. return true;
  319. }
  320. return false;
  321. }
  322. public function setNetwork($network)
  323. {
  324. if (!in_array($network, array(self::NETWORK_PERSONAL, self::NETWORK_PUBLIC)))
  325. {
  326. throw new \Exception("Wrong network set");
  327. }
  328. $this->network = $network;
  329. }
  330. }