|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ColdTrick\TheWireTools\Controllers; |
| 4 | + |
| 5 | +use Elgg\Exceptions\Http\ValidationException; |
| 6 | + |
| 7 | +/** |
| 8 | + * Additions to the core TheWire EditAction |
| 9 | + * - allow group content / access |
| 10 | + * - link reshared content |
| 11 | + */ |
| 12 | +class EditAction extends \Elgg\TheWire\Controllers\EditAction { |
| 13 | + |
| 14 | + /** |
| 15 | + * {@inheritdoc} |
| 16 | + */ |
| 17 | + protected function validate(): void { |
| 18 | + parent::validate(); |
| 19 | + |
| 20 | + // validate container guid (to allow for group content) |
| 21 | + if (elgg_get_plugin_setting('enable_group', 'thewire_tools') !== 'yes') { |
| 22 | + $this->request->setParam('container_guid', null); |
| 23 | + } else { |
| 24 | + $container_guid = (int) $this->request->getParam('container_guid'); |
| 25 | + |
| 26 | + $group = get_entity($container_guid); |
| 27 | + if ($group instanceof \ElggGroup) { |
| 28 | + if (!$group->isToolEnabled('thewire')) { |
| 29 | + // not allowed to post in this group |
| 30 | + throw new ValidationException(elgg_echo('thewire_tools:groups:error:not_enabled')); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + protected function execute(array $skip_field_names = []): void { |
| 40 | + $skip_field_names[] = 'access_id'; |
| 41 | + |
| 42 | + parent::execute($skip_field_names); |
| 43 | + |
| 44 | + // save access ID |
| 45 | + $this->entity->access_id = $this->getAccessID(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + protected function executeAfter(): void { |
| 52 | + parent::executeAfter(); |
| 53 | + |
| 54 | + // save reshare guid |
| 55 | + $reshare_guid = (int) $this->request->getParam('reshare_guid'); |
| 56 | + if ($reshare_guid > 0) { |
| 57 | + $this->entity->addRelationship($reshare_guid, 'reshare'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the correct access_id for content in groups etc |
| 63 | + * |
| 64 | + * @return int |
| 65 | + */ |
| 66 | + protected function getAccessID(): int { |
| 67 | + $access_id = (int) $this->request->getParam('access_id', ACCESS_PUBLIC); |
| 68 | + if ($access_id === -100) { |
| 69 | + $access_id = null; |
| 70 | + } |
| 71 | + |
| 72 | + if (elgg_get_plugin_setting('enable_group', 'thewire_tools') === 'yes') { |
| 73 | + $container_guid = (int) $this->request->getParam('container_guid'); |
| 74 | + |
| 75 | + $group = get_entity($container_guid); |
| 76 | + if ($group instanceof \ElggGroup) { |
| 77 | + $acl = $group->getOwnedAccessCollection('group_acl'); |
| 78 | + if ($acl instanceof \ElggAccessCollection) { |
| 79 | + if (is_null($access_id) || $group->getContentAccessMode() === \ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY) { |
| 80 | + $access_id = $acl->id; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // check the access id |
| 87 | + if ($access_id === ACCESS_PRIVATE) { |
| 88 | + // private wire posts aren't allowed |
| 89 | + $access_id = ACCESS_LOGGED_IN; |
| 90 | + } |
| 91 | + |
| 92 | + if (is_null($access_id)) { |
| 93 | + $access_id = ACCESS_PUBLIC; |
| 94 | + } |
| 95 | + |
| 96 | + return $access_id; |
| 97 | + } |
| 98 | +} |
0 commit comments