PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/model/doctrine/MemberTable.class.php

http://piwam.googlecode.com/
PHP | 328 lines | 161 code | 36 blank | 131 comment | 17 complexity | 4184864b044d3bcc2163073ffdd3cffe MD5 | raw file
Possible License(s): ISC, LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * Doctrine class to retrieve rows of Member table
  4. *
  5. * @author Adrien Mogenet
  6. * @since 1.2
  7. */
  8. class MemberTable extends Doctrine_Table
  9. {
  10. /**
  11. * Defines folder where pictures will be stored
  12. *
  13. * @var string
  14. */
  15. const PICTURE_DIR = 'uploads/trombinoscope';
  16. /**
  17. * Value of state if user account is disabled
  18. *
  19. * @var integer
  20. */
  21. const STATE_DISABLED = 0;
  22. /**
  23. * Value of state if user account is enabled
  24. *
  25. * @var integer
  26. */
  27. const STATE_ENABLED = 1;
  28. /**
  29. * Value of state if user account is pending
  30. *
  31. * @var integer
  32. */
  33. const STATE_PENDING = 2;
  34. /**
  35. * Retrieve list of Member who belong to association $id.
  36. * Used in export feature
  37. *
  38. * @param integer $id
  39. * @return array of Members
  40. */
  41. public static function getEnabledForAssociation($id)
  42. {
  43. $q = self::getQueryEnabledForAssociation($id);
  44. return $q->execute();
  45. }
  46. /**
  47. * Get the query to retrieve Members of association $id
  48. *
  49. * @param integer $id
  50. * @return Doctrine_Query
  51. */
  52. public static function getQueryEnabledForAssociation($id)
  53. {
  54. $q = Doctrine_Query::create()
  55. ->from('Member m')
  56. ->where('m.association_id = ?', $id)
  57. ->andWhere('m.state = ?', self::STATE_ENABLED)
  58. ->orderBy('m.firstname ASC');
  59. return $q;
  60. }
  61. /**
  62. * Build a Doctrine_Query object according to criteria given by
  63. * parameter $params.
  64. * Supported params :
  65. *
  66. * - association_id
  67. * - magic
  68. * - state
  69. * - due_state
  70. * - order_by
  71. *
  72. * @param array $params
  73. * @return Doctrine_Query
  74. */
  75. public static function getQuerySearch($params)
  76. {
  77. $q = Doctrine_Query::create()
  78. ->from('Member m');
  79. /*
  80. * Select only members who belong to a specific association
  81. */
  82. if (isset ($params['association_id']))
  83. {
  84. $q->andWhere('m.association_id = ?', $params['association_id']);
  85. }
  86. /*
  87. * Restrict the research to the enabled/fisabled members
  88. */
  89. if (isset ($params['state']))
  90. {
  91. $q->andWhere('m.state = ?', $params['state']);
  92. }
  93. /*
  94. * Widget 'magic' is used to perform a search on several fields :
  95. * firstname, lastname... and we can add more
  96. */
  97. if (isset ($params['magic']) && $params['magic'] != "")
  98. {
  99. $query = '%' . $params['magic'] . '%';
  100. $q->andWhere("concat(concat(m.firstname, ' '), m.lastname) LIKE ?", $query);
  101. }
  102. /*
  103. * Widget 'due_state' can have different values :
  104. *
  105. * - ok : Select members who don't have to paye their due
  106. * - ko : Select members who have to pay their due
  107. * - month : Select members whom due will expire in a month
  108. */
  109. if (isset ($params['due_state']))
  110. {
  111. $today = date('Y-m-d');
  112. if ($params['due_state'] == 'ok')
  113. {
  114. $q->leftJoin('m.Due d');
  115. $q->leftJoin('d.DueType t');
  116. $q->andWhere('m.due_exempt = ?', true);
  117. $q->orWhere("ADDDATE(d.date, INTERVAL t.period MONTH) >= ?", $today);
  118. }
  119. if ($params['due_state'] == 'ko')
  120. {
  121. $q->leftJoin('m.Due d');
  122. $q->leftJoin('d.DueType t');
  123. $q->andWhere('m.due_exempt = ?', false);
  124. $q->andWhere("(d.date IS NULL OR ADDDATE(d.date, INTERVAL t.period MONTH) < ?)", $today);
  125. }
  126. if ($params['due_state'] == 'month')
  127. {
  128. $q->leftJoin('m.Due d');
  129. $q->leftJoin('d.DueType t');
  130. $q->andWhere('m.due_exempt = ?', false);
  131. $q->andWhere("ADDDATE(d.date, INTERVAL t.period - 1 MONTH) < ?", $today);
  132. $q->andWhere("ADDDATE(d.date, INTERVAL t.period MONTH) >= ?", $today);
  133. }
  134. }
  135. /*
  136. * If a sorting column has been specified, we order the
  137. * result
  138. */
  139. if (isset ($params['order_by']))
  140. {
  141. $column = $params['order_by'];
  142. $sortable_columns = array('lastname', 'firstname', 'username', 'city', 'status_id');
  143. if (! in_array($column, $sortable_columns))
  144. {
  145. $column = 'lastname';
  146. }
  147. $q->orderBy('m.' . $column . ' ASC');
  148. }
  149. return $q;
  150. }
  151. /**
  152. * Retrieve pending user accounts
  153. *
  154. * @param integer $association_id
  155. * @return array of Member
  156. */
  157. public static function getPendingMembers($association_id)
  158. {
  159. $q = Doctrine_Query::create()
  160. ->select('m.*')
  161. ->from('Member m')
  162. ->where('m.association_id = ?', $association_id)
  163. ->andWhere('state = ?', self::STATE_PENDING);
  164. return $q->execute();
  165. }
  166. /**
  167. * Try to select the (unique) user matching $username and
  168. * $password. $password is not crypted, he will be crypted
  169. * into the method. This method won't search disabled
  170. * members.
  171. *
  172. * @param string $username
  173. * @param string $password
  174. * @return Member
  175. */
  176. public static function getByUsernameAndPassword($username, $password)
  177. {
  178. $q = Doctrine_Query::create()
  179. ->select('m.id')
  180. ->from('Member m')
  181. ->where('m.username = ?', $username)
  182. ->andWhere('m.password = ?', sha1($password))
  183. ->andWhere('m.state = ?', self::STATE_ENABLED)
  184. ->limit(1);
  185. return $q->fetchOne();
  186. }
  187. /**
  188. * Retrieve a member by his username
  189. *
  190. * @param string $username
  191. * @return Member
  192. */
  193. public static function getByUsername($username)
  194. {
  195. $q = Doctrine_Query::create()
  196. ->select('m.id')
  197. ->from('Member m')
  198. ->where('m.username = ?', $username)
  199. ->limit(1);
  200. return $q->fetchOne();
  201. }
  202. /**
  203. * Retrieve a member by his username
  204. *
  205. * @param string $username
  206. * @return Member
  207. */
  208. public static function getById($id)
  209. {
  210. $q = Doctrine_Query::create()
  211. ->from('Member m')
  212. ->where('m.id= ?', $id)
  213. ->limit(1);
  214. return $q->fetchOne();
  215. }
  216. /**
  217. * Display Membre matching our query. $query is going to be set as a
  218. * magic criteria that the engine will try to match after comparison
  219. * on several fields.
  220. *
  221. * @param string $query
  222. * @param integer $limit
  223. * @param integer $associationId
  224. * @return array of Member
  225. */
  226. static public function magicSearch($query, $limit, $associationId)
  227. {
  228. $params = array('association_id' => $associationId,
  229. 'state' => self::STATE_ENABLED,
  230. 'magic'=> $query);
  231. $q = self::getQuerySearch($params);
  232. $q->limit($limit);
  233. return $q->execute();
  234. }
  235. /**
  236. * Display Membre matching our query. $query is going to be set as a
  237. * magic criteria that the engine will try to match after comparison
  238. * on several fields.
  239. *
  240. * @param array $params
  241. * @return sfDoctrinePager Paginated list of Member objects
  242. */
  243. static public function search($params, $page = 1)
  244. {
  245. $q = self::getQuerySearch($params);
  246. $n = Configurator::get('users_by_page', $params['association_id'], 20);
  247. if (isset($params['by_page']))
  248. {
  249. if ($params['by_page'] == 'all')
  250. {
  251. $n = 1000; // we set a maximum anyway
  252. }
  253. elseif (is_integer($params['by_page']))
  254. {
  255. $n = $params['by_page'];
  256. }
  257. }
  258. $pager = new sfDoctrinePager('Member', $n);
  259. $pager->setQuery($q);
  260. $pager->setPage($page);
  261. $pager->init();
  262. return $pager;
  263. }
  264. /**
  265. * Retrieve list of users who have an email and belong to association $id
  266. *
  267. * @param integer $id
  268. * @return array of Member
  269. */
  270. public static function getHavingEmailForAssociation($id)
  271. {
  272. $q = Doctrine_Query::create()
  273. ->from('Member m')
  274. ->where('m.association_id = ?', $id)
  275. ->andWhere('m.state = ?', self::STATE_ENABLED)
  276. ->andWhere('m.email IS NOT NULL')
  277. ->andWhere('m.email != ""');
  278. return $q->execute();
  279. }
  280. /**
  281. * Count existing Member
  282. *
  283. * @return integer
  284. */
  285. public static function doCount()
  286. {
  287. $q = Doctrine_Query::create()
  288. ->select('m.id')
  289. ->from('Member m');
  290. return $q->count();
  291. }
  292. }