You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
7.6KB

  1. <?php
  2. namespace domain\User\User;
  3. use domain\User\UserGroup\UserGroup;
  4. use domain\User\UserProducer\UserProducer;
  5. use domain\User\UserUserGroup\UserUserGroup;
  6. use domain\_\AbstractService;
  7. use domain\_\SolverInterface;
  8. class UserSolver extends AbstractService implements SolverInterface
  9. {
  10. public function getTypeChoicesArray(): array
  11. {
  12. return [
  13. User::TYPE_INDIVIDUAL => 'Particulier',
  14. User::TYPE_LEGAL_PERSON => 'Personne morale',
  15. User::TYPE_GUEST => 'Visiteur'
  16. ];
  17. }
  18. public function getTypeLabel(string $type): string
  19. {
  20. $typesArray = $this->getTypeChoicesArray();
  21. if(key_exists($type, $typesArray)) {
  22. return $typesArray[$type];
  23. }
  24. return '';
  25. }
  26. public function getTypeIndex(string $typeLabel = null): ?string
  27. {
  28. foreach($this->getTypeChoicesArray() as $key => $label) {
  29. if($label == $typeLabel) {
  30. return $key;
  31. }
  32. }
  33. return null;
  34. }
  35. public function isTypeValid(string $type = null): bool
  36. {
  37. return in_array($type, User::$types);
  38. }
  39. public function isTypeLegalPerson(User $user): bool
  40. {
  41. return $user->type == User::TYPE_LEGAL_PERSON;
  42. }
  43. public function isTypeIndividual(User $user): bool
  44. {
  45. return $user->type == User::TYPE_INDIVIDUAL;
  46. }
  47. public function isTypeGuest(User $user): bool
  48. {
  49. return $user->type == User::TYPE_GUEST;
  50. }
  51. public function getContactSummaryFromArrayAsHtml(array $user): string
  52. {
  53. $html = '';
  54. if (strlen($user['phone'])) {
  55. $html .= $user['phone'];
  56. }
  57. if (strlen($user['phone']) && strlen($user['email'])) {
  58. $html .= '<br />';
  59. }
  60. if (strlen($user['email'])) {
  61. $html .= $user['email'];
  62. }
  63. return $html;
  64. }
  65. public function getUsername(User $user, $withType = false): string
  66. {
  67. if ($this->isTypeLegalPerson($user)
  68. && isset($user->name_legal_person)
  69. && strlen($user->name_legal_person)) {
  70. $username = $user->name_legal_person;
  71. } else {
  72. $username = $user->lastname . ' ' . $user->name;
  73. }
  74. if ($withType && $this->isTypeLegalPerson($user)) {
  75. $username = $username . ' (personne morale)';
  76. }
  77. return $username;
  78. }
  79. public function getUsernameFromArray(array $modelArray, $withType = false): string
  80. {
  81. $username = 'Nom indéfini';
  82. if ($modelArray['type'] == User::TYPE_LEGAL_PERSON
  83. && isset($modelArray['name_legal_person'])
  84. && strlen($modelArray['name_legal_person'])) {
  85. $username = $modelArray['name_legal_person'];
  86. } elseif((isset($modelArray['lastname']) && $modelArray['lastname'])
  87. || (isset($modelArray['name']) && $modelArray['name'])) {
  88. $username = $modelArray['lastname'] . ' ' . $modelArray['name'];
  89. }
  90. if ($withType && $modelArray['type'] == User::TYPE_LEGAL_PERSON) {
  91. $username = $username . ' (personne morale)';
  92. }
  93. return $username;
  94. }
  95. public function getFullAddress(User $user, $nl2br = false)
  96. {
  97. $address = '';
  98. if (isset($user->lastname) && isset($user->name) && strlen($user->lastname) && strlen($user->name)) {
  99. $address .= $user->lastname . ' ' . $user->name . "\n";
  100. }
  101. if (isset($user->name_legal_person) && strlen($user->name_legal_person)) {
  102. $address .= $user->name_legal_person . "\n";
  103. }
  104. $address .= $user->address;
  105. if ($nl2br) {
  106. $address = nl2br($address);
  107. }
  108. return $address;
  109. }
  110. /**
  111. * Finds out if password reset token is valid
  112. *
  113. * @param string $token password reset token
  114. * @return boolean
  115. */
  116. public function isPasswordResetTokenValid($token)
  117. {
  118. if (empty($token)) {
  119. return false;
  120. }
  121. $expire = \Yii::$app->parameterBag->get('user.passwordResetTokenExpire');
  122. $parts = explode('_', $token);
  123. $timestamp = (int)end($parts);
  124. return $timestamp + $expire >= time();
  125. }
  126. /**
  127. * Retourne si l'utilisateur est un admin ou non.
  128. *
  129. */
  130. public function isAdmin(User $user): bool
  131. {
  132. return $user->status == User::STATUS_ADMIN;
  133. }
  134. public function isStatusAdministrator(User $user): bool
  135. {
  136. return $user->getStatus() == User::STATUS_ADMIN;
  137. }
  138. /**
  139. * Retourne si l'utilisateur est un producteur ou non.
  140. *
  141. */
  142. public function isProducer(User $user): bool
  143. {
  144. return ($user->status == User::STATUS_ADMIN
  145. || $user->status == User::STATUS_PRODUCER) && $user->id_producer;
  146. }
  147. public function isStatusProducer(User $user): bool
  148. {
  149. return $user->getStatus() == User::STATUS_PRODUCER;
  150. }
  151. public function isStatusUser(User $user): bool
  152. {
  153. return $user->getStatus() == User::STATUS_ACTIVE;
  154. }
  155. /**
  156. * Retourne l'utilisateur courant.
  157. *
  158. */
  159. public function getCurrent(): ?User
  160. {
  161. if (!\Yii::$app->user->isGuest) {
  162. return \Yii::$app->user->identity;
  163. }
  164. return null;
  165. }
  166. public function isCurrentGuest(): bool
  167. {
  168. return \Yii::$app->user->isGuest;
  169. }
  170. /**
  171. * Retourne si l'utilisateur courant est connecté ou non.
  172. *
  173. */
  174. public function isCurrentConnected(): bool
  175. {
  176. return !\Yii::$app->user->isGuest;
  177. }
  178. /**
  179. * Retourne l'ID de l'utilisateur courant connecté.
  180. *
  181. */
  182. public function getCurrentId()
  183. {
  184. if (!\Yii::$app->user->isGuest) {
  185. return \Yii::$app->user->identity->id;
  186. }
  187. return false;
  188. }
  189. /**
  190. * Retourne le status de l'utilisateur courant connecté.
  191. *
  192. */
  193. public function getCurrentStatus()
  194. {
  195. if (!\Yii::$app->user->isGuest) {
  196. return \Yii::$app->user->identity->status;
  197. }
  198. return false;
  199. }
  200. public function hasAccessBackend(): bool
  201. {
  202. $userCurrentStatus = $this->getCurrentStatus();
  203. return $userCurrentStatus == User::STATUS_ADMIN
  204. || $userCurrentStatus == User::STATUS_PRODUCER;
  205. }
  206. // belongsToUserGroup
  207. public function isUserBelongsToUserGroup(User $user, UserGroup $userGroup): bool
  208. {
  209. if (!$user->userUserGroup) {
  210. $user->populateRelation('userUserGroup', UserUserGroup::searchAll([
  211. 'id_user' => $user->id
  212. ]));
  213. }
  214. if ($user->userUserGroup) {
  215. foreach ($user->userUserGroup as $userUserGroup) {
  216. if ($userUserGroup->id_user_group == $userGroup->id) {
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. public function getUserProducer(User $user): ?UserProducer
  224. {
  225. $userProducer = null;
  226. $producerId = $this->getProducerContextId();
  227. foreach($user->userProducer as $userProducerRelation) {
  228. if($userProducerRelation->id_producer == $producerId) {
  229. $userProducer = $userProducerRelation;
  230. }
  231. }
  232. return $userProducer;
  233. }
  234. public function getEmailSendingInvoicingDocuments(User $user): ?string
  235. {
  236. if($user->email_sending_invoicing_documents && strlen($user->email_sending_invoicing_documents) > 0) {
  237. return $user->email_sending_invoicing_documents;
  238. }
  239. if($user->email && strlen($user->email) > 0) {
  240. return $user->email;
  241. }
  242. return null;
  243. }
  244. }