ElementSearcher.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace Muzich\CoreBundle\Searcher;
  3. use Symfony\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. 'string' => $this->getString()
  139. );
  140. }
  141. public function getNetwork()
  142. {
  143. return $this->network;
  144. }
  145. public function isNetworkPublic()
  146. {
  147. if ($this->network == self::NETWORK_PUBLIC)
  148. {
  149. return true;
  150. }
  151. return false;
  152. }
  153. public function isNetworkPersonal()
  154. {
  155. if ($this->network == self::NETWORK_PERSONAL)
  156. {
  157. return true;
  158. }
  159. return false;
  160. }
  161. public function setNoTags()
  162. {
  163. $this->tags = array();
  164. }
  165. public function getTags($tags_string = false)
  166. {
  167. if (!$tags_string)
  168. {
  169. return $this->tags;
  170. }
  171. $ids = array();
  172. foreach ($this->tags as $id => $name)
  173. {
  174. $ids[] = $id;
  175. }
  176. return json_encode($ids);
  177. }
  178. public function getCount()
  179. {
  180. return $this->count;
  181. }
  182. public function getUserId()
  183. {
  184. return $this->user_id;
  185. }
  186. public function getIdLimit()
  187. {
  188. return $this->id_limit;
  189. }
  190. public function getGroupId()
  191. {
  192. return $this->group_id;
  193. }
  194. public function isFavorite()
  195. {
  196. return $this->favorite;
  197. }
  198. public function setIds($ids)
  199. {
  200. $this->ids = $ids;
  201. }
  202. public function getIds()
  203. {
  204. return $this->ids;
  205. }
  206. public function hasIds()
  207. {
  208. if (count($this->ids))
  209. {
  210. return true;
  211. }
  212. return false;
  213. }
  214. public function setIdsDisplay($display)
  215. {
  216. $this->ids_display = $display;
  217. }
  218. public function getIdsDisplay()
  219. {
  220. return $this->ids_display;
  221. }
  222. public function setTagStrict($strict)
  223. {
  224. $this->tag_strict = $strict;
  225. }
  226. public function getTagStrict()
  227. {
  228. return $this->tag_strict;
  229. }
  230. public function setString($string)
  231. {
  232. $this->string = $string;
  233. }
  234. public function getString()
  235. {
  236. return $this->string;
  237. }
  238. /**
  239. * Construction de l'objet Query
  240. *
  241. * @param Registry $doctrine
  242. * @param int $user_id
  243. * @param string $exec_type
  244. *
  245. * @return collection
  246. */
  247. protected function constructQueryObject(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  248. {
  249. $this->setQuery($doctrine
  250. ->getRepository('MuzichCoreBundle:Element')
  251. ->findBySearch($this, $user_id, $exec_type, $params))
  252. ;
  253. }
  254. /**
  255. * Retourne l'objet Query
  256. *
  257. * @param Registry $doctrine
  258. * @param int $user_id
  259. * @param string $exec_type
  260. *
  261. * @return collection
  262. */
  263. public function getQuery(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  264. {
  265. $this->constructQueryObject($doctrine, $user_id, $exec_type, $params);
  266. return $this->query;
  267. }
  268. /**
  269. * Retourne les elements correspondant a la recherche
  270. * user_id: Identifiant de celui qui navigue
  271. *
  272. * @param Registry $doctrine
  273. * @param int $user_id
  274. * @param string $exec_type Type d'execution
  275. *
  276. * @return collection
  277. */
  278. public function getElements(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
  279. {
  280. $query = $this->getQuery($doctrine, $user_id, $exec_type, $params);
  281. switch ($exec_type)
  282. {
  283. case 'execute':
  284. return $query->execute();
  285. break;
  286. case 'count':
  287. return count($query->getArrayResult());
  288. break;
  289. case 'single':
  290. return $query->getSingleResult();
  291. break;
  292. default :
  293. throw new \Exception('Mode de récupération des Elements non supporté.');
  294. break;
  295. }
  296. }
  297. public function isSearchingNew()
  298. {
  299. return $this->searchnew;
  300. }
  301. public function isNeedTags()
  302. {
  303. if ($this->need_tags)
  304. {
  305. return true;
  306. }
  307. return false;
  308. }
  309. }