]> git.mxchange.org Git - friendica.git/blob - src/Module/Item/Compose.php
Added post update to remove duplicated contacts
[friendica.git] / src / Module / Item / Compose.php
1 <?php
2
3 namespace Friendica\Module\Item;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Feature;
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\FileTag;
14 use Friendica\Model\Group;
15 use Friendica\Model\Item;
16 use Friendica\Model\User;
17 use Friendica\Module\Login;
18 use Friendica\Network\HTTPException\NotImplementedException;
19 use Friendica\Util\Crypto;
20
21 class Compose extends BaseModule
22 {
23         public static function post()
24         {
25                 if (!empty($_REQUEST['body'])) {
26                         $_REQUEST['return'] = 'network';
27                         require_once 'mod/item.php';
28                         item_post(self::getApp());
29                 } else {
30                         notice(L10n::t('Please enter a post body.'));
31                 }
32         }
33
34         public static function content()
35         {
36                 if (!local_user()) {
37                         return Login::form('compose', false);
38                 }
39
40                 $a = self::getApp();
41
42                 if ($a->getCurrentTheme() !== 'frio') {
43                         throw new NotImplementedException(L10n::t('This feature is only available with the frio theme.'));
44                 }
45
46                 /// @TODO Retrieve parameter from router
47                 $posttype = $a->argv[1] ?? Item::PT_ARTICLE;
48                 if (!in_array($posttype, [Item::PT_ARTICLE, Item::PT_PERSONAL_NOTE])) {
49                         switch ($posttype) {
50                                 case 'note':
51                                         $posttype = Item::PT_PERSONAL_NOTE;
52                                         break;
53                                 default:
54                                         $posttype = Item::PT_ARTICLE;
55                                         break;
56                         }
57                 }
58
59                 $user = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'hidewall', 'default-location']);
60
61                 switch ($posttype) {
62                         case Item::PT_PERSONAL_NOTE:
63                                 $compose_title = L10n::t('Compose new personal note');
64                                 $type = 'note';
65                                 $doesFederate = false;
66                                 $contact_allow = $a->contact['id'];
67                                 $group_allow = '';
68                                 break;
69                         default:
70                                 $compose_title = L10n::t('Compose new post');
71                                 $type = 'post';
72                                 $doesFederate = true;
73                                 $contact_allow = implode(',', expand_acl($user['allow_cid']));
74                                 $group_allow = implode(',', expand_acl($user['allow_gid'])) ?: Group::FOLLOWERS;
75                                 break;
76                 }
77
78                 $title         = $_REQUEST['title']         ?? '';
79                 $category      = $_REQUEST['category']      ?? '';
80                 $body          = $_REQUEST['body']          ?? '';
81                 $location      = $_REQUEST['location']      ?? $user['default-location'];
82                 $wall          = $_REQUEST['wall']          ?? $type == 'post';
83                 $contact_allow = $_REQUEST['contact_allow'] ?? $contact_allow;
84                 $group_allow   = $_REQUEST['group_allow']   ?? $group_allow;
85                 $contact_deny  = $_REQUEST['contact_deny']  ?? implode(',', expand_acl($user['deny_cid']));
86                 $group_deny    = $_REQUEST['group_deny']    ?? implode(',', expand_acl($user['deny_gid']));
87                 $visibility    = ($contact_allow . $user['allow_gid'] . $user['deny_cid'] . $user['deny_gid']) ? 'custom' : 'public';
88
89                 $acl_contacts = Contact::selectToArray(['id', 'name', 'addr', 'micro'], ['uid' => local_user(), 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
90                 array_walk($acl_contacts, function (&$value) {
91                         $value['type'] = 'contact';
92                 });
93
94                 $acl_groups = [
95                         [
96                                 'id' => Group::FOLLOWERS,
97                                 'name' => L10n::t('Followers'),
98                                 'addr' => '',
99                                 'micro' => 'images/twopeople.png',
100                                 'type' => 'group',
101                         ],
102                         [
103                                 'id' => Group::MUTUALS,
104                                 'name' => L10n::t('Mutuals'),
105                                 'addr' => '',
106                                 'micro' => 'images/twopeople.png',
107                                 'type' => 'group',
108                         ]
109                 ];
110                 foreach (Group::getByUserId(local_user()) as $group) {
111                         $acl_groups[] = [
112                                 'id' => $group['id'],
113                                 'name' => $group['name'],
114                                 'addr' => '',
115                                 'micro' => 'images/twopeople.png',
116                                 'type' => 'group',
117                         ];
118                 }
119
120                 $acl = array_merge($acl_groups, $acl_contacts);
121
122                 $jotnets_fields = [];
123                 $mail_enabled = false;
124                 $pubmail_enabled = false;
125                 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
126                         $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
127                         if (DBA::isResult($mailacct)) {
128                                 $mail_enabled = true;
129                                 $pubmail_enabled = !empty($mailacct['pubmail']);
130                         }
131                 }
132
133                 if (empty($user['hidewall'])) {
134                         if ($mail_enabled) {
135                                 $jotnets_fields[] = [
136                                         'type' => 'checkbox',
137                                         'field' => [
138                                                 'pubmail_enable',
139                                                 L10n::t('Post to Email'),
140                                                 $pubmail_enabled
141                                         ]
142                                 ];
143                         }
144
145                         Hook::callAll('jot_networks', $jotnets_fields);
146                 }
147
148                 $jotplugins = '';
149                 Hook::callAll('jot_tool', $jotplugins);
150
151                 // Output
152
153                 $a->registerFooterScript('view/js/ajaxupload.js');
154                 $a->registerFooterScript('view/js/linkPreview.js');
155                 $a->registerFooterScript('view/asset/typeahead.js/dist/typeahead.bundle.js');
156                 $a->registerFooterScript('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput.js');
157                 $a->registerStylesheet('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput.css');
158                 $a->registerStylesheet('view/theme/frio/frameworks/friendica-tagsinput/friendica-tagsinput-typeahead.css');
159
160                 $tpl = Renderer::getMarkupTemplate('item/compose-footer.tpl');
161                 $a->page['footer'] .= Renderer::replaceMacros($tpl, [
162                         '$acl_contacts' => $acl_contacts,
163                         '$acl_groups' => $acl_groups,
164                         '$acl' => $acl,
165                 ]);
166
167                 $tpl = Renderer::getMarkupTemplate('item/compose.tpl');
168                 return Renderer::replaceMacros($tpl, [
169                         '$compose_title'=> $compose_title,
170                         '$id'           => 0,
171                         '$posttype'     => $posttype,
172                         '$type'         => $type,
173                         '$wall'         => $wall,
174                         '$default'      => '',
175                         '$mylink'       => $a->removeBaseURL($a->contact['url']),
176                         '$mytitle'      => L10n::t('This is you'),
177                         '$myphoto'      => $a->removeBaseURL($a->contact['thumb']),
178                         '$submit'       => L10n::t('Submit'),
179                         '$edbold'       => L10n::t('Bold'),
180                         '$editalic'     => L10n::t('Italic'),
181                         '$eduline'      => L10n::t('Underline'),
182                         '$edquote'      => L10n::t('Quote'),
183                         '$edcode'       => L10n::t('Code'),
184                         '$edimg'        => L10n::t('Image'),
185                         '$edurl'        => L10n::t('Link'),
186                         '$edattach'     => L10n::t('Link or Media'),
187                         '$prompttext'   => L10n::t('Please enter a image/video/audio/webpage URL:'),
188                         '$preview'      => L10n::t('Preview'),
189                         '$location_set' => L10n::t('Set your location'),
190                         '$location_clear' => L10n::t('Clear the location'),
191                         '$location_unavailable' => L10n::t('Location services are unavailable on your device'),
192                         '$location_disabled' => L10n::t('Location services are disabled. Please check the website\'s permissions on your device'),
193                         '$wait'         => L10n::t('Please wait'),
194                         '$placeholdertitle' => L10n::t('Set title'),
195                         '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t('Categories (comma-separated list)') : ''),
196                         '$public_title'  => L10n::t('Public'),
197                         '$public_desc'  => L10n::t('This post will be sent to all your followers and can be seen in the community pages and by anyone with its link.'),
198                         '$custom_title' => L10n::t('Limited/Private'),
199                         '$custom_desc'  => L10n::t('This post will be sent only to the people in the first box, to the exception of the people mentioned in the second box. It won\'t appear anywhere public.'),
200                         '$emailcc'      => L10n::t('CC: email addresses'),
201                         '$title'        => $title,
202                         '$category'     => $category,
203                         '$body'         => $body,
204                         '$location'     => $location,
205                         '$visibility'   => $visibility,
206                         '$contact_allow'=> $contact_allow,
207                         '$group_allow'  => $group_allow,
208                         '$contact_deny' => $contact_deny,
209                         '$group_deny'   => $group_deny,
210                         '$jotplugins'   => $jotplugins,
211                         '$doesFederate' => $doesFederate,
212                         '$jotnets_fields'=> $jotnets_fields,
213                         '$sourceapp'    => L10n::t($a->sourcename),
214                         '$rand_num'     => Crypto::randomDigits(12)
215                 ]);
216         }
217 }