Guest User

Untitled

a guest
Apr 18th, 2018
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. <?php
  2. // +---------------------------------------------------------------------------+
  3. // | This file is part of a Agavi Propel Mootools (APM) Project. |
  4. // | Copyright (C) Jean-Philippe Dery (jeanphilippe.dery@gmail.com) |
  5. // | |
  6. // | For the full copyright and license information, please view the LICENSE |
  7. // | file that was distributed with this source code. |
  8. // +---------------------------------------------------------------------------+
  9.  
  10. /**
  11. * ApmMemberModel model is the business logic around a member.
  12. * @package model
  13. * @subpackage models
  14. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  15. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  16. * @since 1.0.0
  17. * @version 1.0.0
  18. */
  19. class ApmMemberModel extends ApmBaseOmModel
  20. {
  21. /**
  22. * @constant int The developer type id.
  23. */
  24. const TYPE_DEVELOPER = 1;
  25.  
  26. /**
  27. * @constant int The customer type id.
  28. */
  29. const TYPE_CUSTOMER = 2;
  30.  
  31. /**
  32. * Initialize this model.
  33. * @param array [om] To specify the default record.
  34. * @return void
  35. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  36. * @since 1.0.0
  37. */
  38. public function initialize(AgaviContext $context, array $parameters = array())
  39. {
  40. parent::initialize($context, $parameters);
  41. $this->setObjectModel(isset($parameters['om']) ? $parameters['om'] : new DeskMember());
  42. }
  43.  
  44. /**
  45. * Return the member's full name.
  46. * @return string The member's full name.
  47. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  48. * @since 1.0.0
  49. */
  50. public function getName()
  51. {
  52. return sprintf('%1$s %2$s',
  53. $this->getFirstname(),
  54. $this->getLastname()
  55. );
  56. }
  57.  
  58. /**
  59. * Return the name associated with the member type.
  60. * @return string The type's name.
  61. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  62. * @since 1.0.0
  63. */
  64. public function getTypeName()
  65. {
  66. switch ($this->getType()) {
  67. case self::TYPE_DEVELOPER : return 'Developer';
  68. case self::TYPE_CUSTOMER : return 'Customer';
  69. }
  70. return '';
  71. }
  72.  
  73. /**
  74. * If the object is new, it inserts it; otherwise an update is performed and
  75. * the date of the update is saved.
  76. * @return int The number of rows affected by this insert/update.
  77. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  78. * @since 1.0.0
  79. */
  80. public function save()
  81. {
  82. $this->setDateUpdated(new DateTime());
  83. return parent::save();
  84. }
  85. }
  86. ?>
  87.  
  88. <?php
  89. // +---------------------------------------------------------------------------+
  90. // | This file is part of a Agavi Propel Mootools (APM) Project. |
  91. // | Copyright (C) Jean-Philippe Dery (jeanphilippe.dery@gmail.com) |
  92. // | |
  93. // | For the full copyright and license information, please view the LICENSE |
  94. // | file that was distributed with this source code. |
  95. // +---------------------------------------------------------------------------+
  96.  
  97. /**
  98. * ApmMembersModel model is the business logic around a member.
  99. * @package model
  100. * @subpackage models
  101. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  102. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  103. * @since 1.0.0
  104. * @version 1.0.0
  105. */
  106. class ApmMembersModel extends ApmBaseModel
  107. {
  108. /**
  109. * Create a new member.
  110. * @param int The member type.
  111. * @param array The members attributes.
  112. * @return object The member.
  113. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  114. * @since 1.0.0
  115. */
  116. public function create($type, array $attrs)
  117. {
  118. $member = $this->context->getModel('ApmMember');
  119. $member->fromArray($attrs);
  120. $member->setType($type);
  121. $member->setDateCreated(new DateTime());
  122. $member->save();
  123. return $member;
  124. }
  125.  
  126. /**
  127. * Create a new member with an administrator status.
  128. * @param array The members attributes.
  129. * @return object The member.
  130. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  131. * @since 1.0.0
  132. */
  133. public function createAdministratorMember(array $attrs)
  134. {
  135. return $this->create(ApmMemberModel::TYPE_ADMINISTRATOR, $attrs);
  136. }
  137.  
  138. /**
  139. * Create a new member with a developer status.
  140. * @param array The members attributes.
  141. * @return object The member.
  142. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  143. * @since 1.0.0
  144. */
  145. public function createDeveloperMember(array $attrs)
  146. {
  147. return $this->create(ApmMemberModel::TYPE_DEVELOPER, $attrs);
  148. }
  149.  
  150. /**
  151. * Create a new member with a customer status.
  152. * @param int The customer he is associated with.
  153. * @param array The members attributes.
  154. * @return object The member.
  155. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  156. * @since 1.0.0
  157. */
  158. public function createCustomerMember($customerId, array $attrs)
  159. {
  160. // TODO
  161. }
  162.  
  163. /**
  164. * Retrieve a member based on his id.
  165. * @param int The member id.
  166. * @return object The member.
  167. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  168. * @since 1.0.0
  169. */
  170. public function getMemberById($memberId)
  171. {
  172. if ($member = DeskMemberPeer::retrieveByPk($memberId)) {
  173. return $this->context->getModel('ApmMember', array('om' => $member));
  174. }
  175. return null;
  176. }
  177.  
  178. /**
  179. * Retrieves members based on their ids.
  180. * @param array The ids.
  181. * @return array The members.
  182. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  183. * @since 1.0.0
  184. */
  185. public function getMemberByIds(array $memberIds)
  186. {
  187. $members = array();
  188. $criteria = new Criteria();
  189. $criteria->add(DeskMemberPeer::ID, $memberIds, Criteria::IN);
  190. foreach (DeskMemberPeer::doSelect($criteria) as $member) {
  191. $members[] = $this->context->getModel('ApmMember', array('om' => $member));
  192. }
  193. return $members;
  194. }
  195.  
  196. /**
  197. * Retrieve a member based on his username and password
  198. * @param string The username.
  199. * @param string The password.
  200. * @param bool True to encode the password.
  201. * @return mixed The member or false if it failed.
  202. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  203. * @since 1.0.0
  204. */
  205. public function getMemberByUsername($username, $password, $encode = true)
  206. {
  207. $password = $encode ? md5($password) : $password;
  208. $criteria = new Criteria();
  209. $criteria->add(DeskMemberPeer::USERNAME, $username);
  210. $criteria->add(DeskMemberPeer::PASSWORD, $password);
  211. if ($member = DeskMemberPeer::doSelectOne($criteria)) {
  212. return $this->context->getModel('ApmMember', array('om' => $member));
  213. }
  214. return null;
  215. }
  216. }
  217. ?>
Add Comment
Please, Sign In to add comment