ElementSearcher.php 7.5KB

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