File: SpotPage_editfilter.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (181 lines) | stat: -rwxr-xr-x 6,509 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class SpotPage_editfilter extends SpotPage_Abs {
	private $_editFilterForm;
	private $_filterId;
	private $_orderList;
	private $_search;
	private $_sorton;
	private $_sortorder;
	private $_data;
	
	function __construct(SpotDb $db, SpotSettings $settings, $currentSession, $params) {
		parent::__construct($db, $settings, $currentSession);
		$this->_editFilterForm = $params['editfilterform'];
		$this->_filterId = $params['filterid'];
		$this->_orderList = $params['orderfilterslist'];
		$this->_search = $params['search'];
		$this->_sorton = $params['sorton'];
		$this->_sortorder = $params['sortorder'];
		$this->_data = $params['data'];
	} # ctor

	function render() {
		$formMessages = array('errors' => array(),
							  'info' => array());
							  
		# Controleer de users' rechten
		$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_filters, '');
		
		# editfilter resultaat is standaard niet geprobeerd
		$editResult = array();

		# Instantieer het Spot user system
		$spotUserSystem = new SpotUserSystem($this->_db, $this->_settings);
		
		# zet de page title
		$this->_pageTitle = "spot: filters";
		
		# haal de te editten filter op 
		$spotFilter = $spotUserSystem->getFilter($this->_currentSession['user']['userid'], $this->_filterId);
		
		/* 
		 * bring the forms' action into the local scope for 
		 * easier access
		 */
		$formAction = $this->_editFilterForm['action'];

		# als de te wijzigen security group niet gevonden kan worden,
		# geef dan een error
		if ((empty($spotFilter)) && ($formAction == 'changefilter')) {
			$editResult = array('result' => 'failure');
			$formMessages['errors'][] = _("Filter doesn't exist");
		} # if

		
		# Is dit een submit van een form, of nog maar de aanroep?
		if ((!empty($formAction)) && (empty($formMessages['errors']))) {
			switch($formAction) {
				case 'removefilter' : {
					$spotUserSystem->removeFilter($this->_currentSession['user']['userid'], $this->_filterId);
					$editResult = array('result' => 'success');
					
					break;
				} # case 'removefilter'
				
				case 'discardfilters' : {
					$spotUserSystem->resetFilterList($this->_currentSession['user']['userid']);
					$editResult = array('result' => 'success');
					
					break;
				} # case 'discardfilters'
				
				case 'setfiltersasdefault' : {
					$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_set_filters_as_default, '');

					$spotUserSystem->setFiltersAsDefault($this->_currentSession['user']['userid']);
					$editResult = array('result' => 'success');
					
					break;
				} # case 'setfiltersasdefault'

				case 'exportfilters': {
					$editResult = $spotUserSystem->filtersToXml($spotUserSystem->getPlainFilterList($this->_currentSession['user']['userid'], 'filter'));
					
					break;
				} # case 'exportfilters' 

				case 'importfilters': {
					if (isset($_FILES['filterimport'])) {
						
						if ($_FILES['filterimport']['error'] === UPLOAD_ERR_OK) {
							$xml = file_get_contents($_FILES['filterimport']['tmp_name']);
							try {
								$filterList = $spotUserSystem->xmlToFilters($xml);
								$spotUserSystem->setFilterList($this->_currentSession['user']['userid'], $filterList);
							} catch(Exception $x) {
								$editResult = array('result' => 'failure');
								$formMessages['errors'][] = _('Uploaded Spotwebfilter in invalid');
							} # catch
						} else {
							$editResult = array('result' => 'failure');
							$formMessages['errors'][] = sprintf(_('Error while uploading filter (%s)'), $_FILES['filterimport']['error']);
						} # if
					
					} else {
						$editResult = array('result' => 'failure');
						$formMessages['errors'][] = _("Filter hasn't been uploaded");
					} # else
					
					break;
				} # case 'importfilters' 
				
				case 'addfilter'	: {
					# Creeer een nieuw filter record - we voegen een filter altijd aan de root toe
					$filter = $this->_editFilterForm;
					$filter['valuelist'] = explode('&', $filter['valuelist']) ;
					$filter['torder'] = 999;
					$filter['tparent'] = 0;
					$filter['children'] = array();
					$filter['filtertype'] = 'filter';
					$filter['sorton'] = $filter['sorton'];
					$filter['sortorder'] = $filter['sortorder'];
					$filter['enablenotify'] = isset($filter['enablenotify']) ? true : false;
						
					# en probeer de filter toe te voegen
					$formMessages['errors'] = $spotUserSystem->addFilter($this->_currentSession['user']['userid'], $filter);
					
					if (!empty($formMessages['errors'])) {
						$editResult = array('result' => 'failure');
					} else {
						$editResult = array('result' => 'success');
					} # else
					
					break;
				} # case 'addfilter' 

				case 'reorder' : {
					$orderCounter = 0;
					
					# Omdat de nestedSortable jquery widget niet een expliciete sortering meegeeft, voegen
					# we die zelf toe aan de hand van hoe de elementen binnen komen
					foreach($this->_orderList as $id => $parent) {
						$spotFilter = $spotUserSystem->getFilter($this->_currentSession['user']['userid'], $id);

						# Als de volgorde of hierarchie dan moet de filter geupdate worden
						if (($spotFilter['torder'] <> $orderCounter) || ($spotFilter['tparent'] <> $parent)) { 
							$spotFilter['torder'] = (int) $orderCounter;
							$spotFilter['tparent'] = (int) $parent;
							$spotUserSystem->changeFilter($this->_currentSession['user']['userid'], $spotFilter);
						} # if
						
						$orderCounter++;
					} # foreach
				} # case 'reorder' 
				
				case 'changefilter'	: {
					$spotFilter = array_merge($spotFilter, $this->_editFilterForm);
					
					$spotUserSystem->changeFilter($this->_currentSession['user']['userid'],
												  $spotFilter);
					$editResult = array('result' => 'success');

					break;
				} # case 'changefilter' 
				
			} # switch
		} # if

		#- display stuff -#
		$this->template('editfilter', array('filter' => $spotFilter,
											'sorton' => $this->_sorton,
											'sortorder' => $this->_sortorder,
											'sortby' => $this->_sorton,
											'sortdir' => $this->_sortorder,
											'lastformaction' => $formAction,
										    'formmessages' => $formMessages,
										    'data' => $this->_data,
											'http_referer' => $this->_editFilterForm['http_referer'],
											'editresult' => $editResult));
	} # render
	
} # class SpotPage_editfilter