Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usersignature.php
1<?php
2
4
10
11class UserSignature extends Base
12{
14
19 public function addAction(array $fields)
20 {
21 $unsafeFields = (array) $this->getRequest()->getPostList()->getRaw('fields');
22 \CUtil::decodeUriComponent($unsafeFields);
23
24 if (($limit = Main\Config\Option::get('mail', 'user_signatures_limit', static::USER_SIGNATURES_LIMIT)) > 0)
25 {
26 $count = UserSignatureTable::getCount(array(
27 'USER_ID' => CurrentUser::get()->getId(),
28 ));
29 if ($count >= $limit)
30 {
31 Loc::loadMessages(__FILE__);
32 $this->errorCollection[] = new Error(Loc::getMessage('MAIL_USER_SIGNATURE_LIMIT'));
33 return false;
34 }
35 }
36
37 $userSignature = new \Bitrix\Mail\Internals\Entity\UserSignature;
38
39 $userSignature->set('USER_ID', CurrentUser::get()->getId());
40 $userSignature->set('SENDER', $fields['sender']);
41 $userSignature->set('SIGNATURE', $this->sanitize($unsafeFields['signature']));
42
43 $result = $userSignature->save();
44
45 if($result->isSuccess())
46 {
47 $userSignature = UserSignatureTable::getById($result->getId())->fetchObject();
48 return $this->getAction($userSignature);
49 }
50 else
51 {
52 $this->errorCollection = $result->getErrors();
53 return false;
54 }
55 }
56
57 protected function checkAccess(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature): bool
58 {
59 if ($userSignature->getUserId() === (int) $this->getCurrentUser()->getId())
60 {
61 return true;
62 }
63
64 $this->addError(new Error(Loc::getMessage('MAIL_USER_SIGNATURE_ACCESS_DENIED')));
65 return false;
66 }
67
71 public function deleteAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature): bool
72 {
73 if (!$this->checkAccess($userSignature))
74 {
75 return false;
76 }
77
78 $userSignature->delete();
79 return true;
80 }
81
82 public function getAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature): bool|array
83 {
84 if (!$this->checkAccess($userSignature))
85 {
86 return false;
87 }
88
89 return [
90 'userSignature' => $this->convertArrayKeysToCamel($userSignature->collectValues(), 1),
91 ];
92 }
93
99 public function updateAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature, array $fields): bool|array
100 {
101 if (!$this->checkAccess($userSignature))
102 {
103 return false;
104 }
105
106 $unsafeFields = (array) $this->getRequest()->getPostList()->getRaw('fields');
107 \CUtil::decodeUriComponent($unsafeFields);
108
109 $userSignature->set('SENDER', $fields['sender']);
110 $userSignature->set('SIGNATURE', $this->sanitize($unsafeFields['signature']));
111
112 $result = $userSignature->save();
113 if($result->isSuccess())
114 {
115 return $this->getAction($userSignature);
116 }
117 else
118 {
119 $this->errorCollection = $result->getErrors();
120 return false;
121 }
122 }
123}
convertArrayKeysToCamel(array $array, $levels=0, $currentLevel=0)
Definition base.php:46
checkAccess(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature)
deleteAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature)
getAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature)
updateAction(\Bitrix\Mail\Internals\Entity\UserSignature $userSignature, array $fields)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29