| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2016, ownCloud, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Joas Schilling <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Lukas Reschke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | namespace OCA\Activity\Settings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCA\Activity\CurrentUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\Activity\UserSettings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\Activity\IExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\Activity\IManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\Activity\ISetting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\AppFramework\Http\TemplateResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\IL10N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\Settings\ISettings; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | class Personal implements ISettings { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var \OCP\IConfig */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	protected $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	/** @var IManager */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	protected $manager; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	/** @var \OCA\Activity\UserSettings */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	protected $userSettings; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	/** @var \OCP\IL10N */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	protected $l10n; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	/** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	protected $user; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	 * constructor of the controller | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param IConfig $config | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param IManager $manager | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param UserSettings $userSettings | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param IL10N $l10n | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param CurrentUser $currentUser | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication | 	public function __construct(IConfig $config, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 								IManager $manager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 								UserSettings $userSettings, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 								IL10N $l10n, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 								CurrentUser $currentUser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->manager = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->userSettings = $userSettings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->l10n = $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->user = (string) $currentUser->getUID(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return TemplateResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	public function getForm() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$settings = $this->manager->getSettings(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 78 |  | View Code Duplication | 		usort($settings, function(ISetting $a, ISetting $b) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			if ($a->getPriority() === $b->getPriority()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				return $a->getIdentifier() > $b->getIdentifier(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			return $a->getPriority() > $b->getPriority(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$activities = []; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 87 |  | View Code Duplication | 		foreach ($settings as $setting) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			if (!$setting->canChangeStream() && !$setting->canChangeMail()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				// No setting can be changed => don't display | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$methods = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			if ($setting->canChangeStream()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				$methods[] = IExtension::METHOD_STREAM; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			if ($setting->canChangeMail()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				$methods[] = IExtension::METHOD_MAIL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			$identifier = $setting->getIdentifier(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$activities[$identifier] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				'desc'		=> $setting->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				IExtension::METHOD_MAIL		=> $this->userSettings->getUserSetting($this->user, 'email', $identifier), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 				IExtension::METHOD_STREAM	=> $this->userSettings->getUserSetting($this->user, 'stream', $identifier), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 				'methods'	=> $methods, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$settingBatchTime = UserSettings::EMAIL_SEND_HOURLY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$currentSetting = (int) $this->userSettings->getUserSetting($this->user, 'setting', 'batchtime'); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 113 |  | View Code Duplication | 		if ($currentSetting === 3600 * 24 * 7) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} else if ($currentSetting === 3600 * 24) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			$settingBatchTime = UserSettings::EMAIL_SEND_DAILY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		} else if ($currentSetting === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			$settingBatchTime = UserSettings::EMAIL_SEND_ASAP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$emailEnabled = $this->config->getAppValue('activity', 'enable_email', 'yes') === 'yes'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		if ($emailEnabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$methods = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				IExtension::METHOD_MAIL => $this->l10n->t('Mail'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				IExtension::METHOD_STREAM => $this->l10n->t('Stream'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$methods = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				IExtension::METHOD_STREAM => $this->l10n->t('Stream'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		return new TemplateResponse('activity', 'settings/personal', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			'setting'			=> 'personal', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			'activities'		=> $activities, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			'is_email_set'		=> !empty($this->config->getUserValue($this->user, 'settings', 'email', '')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			'email_enabled'		=> $emailEnabled, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			'setting_batchtime'	=> $settingBatchTime, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			'notify_self'		=> $this->userSettings->getUserSetting($this->user, 'setting', 'self'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			'notify_selfemail'	=> $this->userSettings->getUserSetting($this->user, 'setting', 'selfemail'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			'methods'			=> $methods, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		], 'blank'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  | 	 * @return string the section ID, e.g. 'sharing' | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	public function getSection() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		return 'activity'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 	 * @return int whether the form should be rather on the top or bottom of | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  | 	 * the admin section. The forms are arranged in ascending order of the | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  | 	 * priority values. It is required to return a value between 0 and 100. | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  | 	 * E.g.: 70 | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	public function getPriority() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		return 55; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.