Changeset 16374
- Timestamp:
- 05/14/08 11:16:35 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/MidCOM_2_8/net.nemein.calendar/config/config.inc
r15556 r16374 35 35 36 36 'language' => null, 37 'allow_name_change' => false, // WARNING: Enabling this enables you to shoot your own foot branches/MidCOM_2_8/net.nemein.calendar/event.php
r16251 r16374 15 15 class net_nemein_calendar_event_dba extends __net_nemein_calendar_event_dba 16 16 { 17 var $_config = false; 18 17 19 function net_nemein_calendar_event_dba($guid = null) 18 20 { 19 return parent::__net_nemein_calendar_event_dba($guid); 21 $stat = parent::__net_nemein_calendar_event_dba($guid); 22 $interface =& $_MIDCOM->componentloader->get_interface_class('net.nemein.calendar'); 23 $this->_config = $interface->get_config_for_topic(); 24 return $stat; 20 25 } 21 26 … … 139 144 } 140 145 146 /** 147 * Checks that the event time range is sane 148 * @return bool indicating sanity 149 */ 141 150 function _check_time_range() 142 151 { 152 static $valid_date_format = '/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/'; 153 // Safety against stupid mistakes (probably asgard) 154 if ( !preg_match($valid_date_format, $this->start) 155 && is_numeric($this->start)) 156 { 157 debug_push_class(__CLASS__, __FUNCTION__); 158 debug_add("Start looks like UNIX timestamp, must be ISO date, rewriting ({$this->start})", MIDCOM_LOG_WARN); 159 debug_pop(); 160 $this->start = date('Y-m-d H:i:s', $this->start); 161 } 162 if ( !preg_match($valid_date_format, $this->end) 163 && is_numeric($this->end)) 164 { 165 debug_push_class(__CLASS__, __FUNCTION__); 166 debug_add("End looks like UNIX timestamp, must be ISO date, rewriting ({$this->end})", MIDCOM_LOG_WARN); 167 debug_pop(); 168 $this->end = date('Y-m-d H:i:s', $this->end); 169 } 170 // Final format safeguard 171 if ( !preg_match($valid_date_format, $this->start) 172 || !preg_match($valid_date_format, $this->end)) 173 { 174 debug_push_class(__CLASS__, __FUNCTION__); 175 debug_add("Start or end not in valid format ({$this->start} & {$this->end})", MIDCOM_LOG_ERROR); 176 debug_pop(); 177 return false; 178 } 143 179 if ( $this->start === '0000-00-00 00:00:00' 144 180 && $this->end === '0000-00-00 00:00:00') … … 151 187 if ($end_comparable < $start_comparable) 152 188 { 189 debug_push_class(__CLASS__, __FUNCTION__); 190 debug_add("Cannot end before starting ({$end_comparable} < {$start_comparable})", MIDCOM_LOG_ERROR); 191 debug_pop(); 153 192 mgd_set_errno(MGD_ERR_RANGE); 154 193 return false; 155 194 } 156 195 // Avoid problems with events too close to the epoch (highly unlikely usage scenario in any case) 157 $epoch = '19720102000000';196 static $epoch = '19720102000000'; 158 197 if ( $start_comparable < $epoch 159 198 || $end_comparable < $epoch) 160 199 { 200 debug_push_class(__CLASS__, __FUNCTION__); 201 debug_add("start or end less than epoch ({$start_comparable} < {$epoch} || {$end_comparable} < {$epoch})", MIDCOM_LOG_ERROR); 202 debug_pop(); 161 203 mgd_set_errno(MGD_ERR_RANGE); 162 204 return false; … … 165 207 } 166 208 209 /** 210 * Checks that the event name is unique (in node/event tree) 211 * @return bool indicating uniqueness 212 */ 213 function name_is_unique() 214 { 215 if (empty($this->name)) 216 { 217 return true; 218 } 219 $qb = new midgard_query_builder('net_nemein_calendar_event'); 220 if (!empty($this->id)) 221 { 222 $qb->add_constraint('id', '<>', $this->id); 223 } 224 $qb->add_constraint('name', '=', $this->name); 225 $qb->add_constraint('node', '=', $this->node); 226 if (!empty($this->up)) 227 { 228 $qb->add_constraint('up', '=', $this->up); 229 } 230 231 $results = $qb->count(); 232 unset($qb); 233 234 if ($results === false) 235 { 236 // QB error (I wonder if this raises some specific error ? 237 //mgd_set_errno(MGD_ERR_ERROR); 238 return false; 239 } 240 241 if ($results > 0) 242 { 243 mgd_set_errno(MGD_ERR_OBJECT_NAME_EXISTS); 244 return false; 245 } 246 247 return true; 248 } 249 167 250 function _on_creating() 168 251 { 169 return $this->_check_time_range(); 252 if (!$this->_config->get('allow_name_change')) 253 { 254 $this->name = ''; 255 } 256 257 if (!$this->_check_time_range()) 258 { 259 return false; 260 } 261 if (!$this->name_is_unique()) 262 { 263 return false; 264 } 265 return true; 170 266 } 171 267 172 268 function _on_updating() 173 269 { 174 return $this->_check_time_range(); 270 if ( !$this->_config->get('allow_name_change') 271 && !isset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}'])) 272 { 273 $this->name = ''; 274 } 275 276 if (!$this->_check_time_range()) 277 { 278 debug_push_class(__CLASS__, __FUNCTION__); 279 debug_add("\$this->_check_time_range() returned false on #{$this->id}", MIDCOM_LOG_ERROR); 280 debug_pop(); 281 return false; 282 } 283 if (!$this->name_is_unique()) 284 { 285 debug_push_class(__CLASS__, __FUNCTION__); 286 debug_add("\$this->name_is_unique() returned false on #{$this->id}", MIDCOM_LOG_ERROR); 287 debug_pop(); 288 return false; 289 } 290 return true; 175 291 } 176 292 … … 180 296 { 181 297 debug_push_class(__CLASS__, __FUNCTION__); 298 debug_add("Detected _on_created loop on #{$this->id}", MIDCOM_LOG_ERROR); 299 debug_pop(); 300 } 301 else 302 { 303 $GLOBALS['net_nemein_calendar_event_dba__on_created_loop_{$this->guid}'] = true; 304 if ( !$this->_config->get('allow_name_change') 305 || empty($this->name)) 306 { 307 /* 308 debug_push_class(__CLASS__, __FUNCTION__); 309 debug_add("Calling midcom_baseclasses_core_dbobject::generate_urlname(\$this) on #{$this->id}"); 310 debug_pop(); 311 */ 312 midcom_baseclasses_core_dbobject::generate_urlname($this); 313 } 314 unset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}']); 315 } 316 return true; 317 } 318 319 function _on_updated() 320 { 321 if (isset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}'])) 322 { 323 debug_push_class(__CLASS__, __FUNCTION__); 182 324 debug_add("Detected _on_updated loop on #{$this->id}", MIDCOM_LOG_ERROR); 183 325 debug_pop(); … … 185 327 else 186 328 { 187 $GLOBALS['net_nemein_calendar_event_dba__on_created_loop_{$this->guid}'] = true;188 midcom_baseclasses_core_dbobject::generate_urlname($this);189 unset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}']);190 }191 return true;192 }193 194 function _on_updated()195 {196 if (isset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}']))197 {198 debug_push_class(__CLASS__, __FUNCTION__);199 debug_add("Detected _on_updated loop on #{$this->id}", MIDCOM_LOG_ERROR);200 debug_pop();201 }202 else203 {204 329 $GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}'] = true; 205 midcom_baseclasses_core_dbobject::generate_urlname($this); 330 if ( !$this->_config->get('allow_name_change') 331 || empty($this->name)) 332 { 333 /* 334 debug_push_class(__CLASS__, __FUNCTION__); 335 debug_add("Calling midcom_baseclasses_core_dbobject::generate_urlname(\$this) on #{$this->id}"); 336 debug_pop(); 337 */ 338 midcom_baseclasses_core_dbobject::generate_urlname($this); 339 } 206 340 unset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}']); 207 341 } branches/MidCOM_2_8/net.nemein.calendar/handler/create.php
r16251 r16374 42 42 $this->_request_data['event']->start = $_POST['start']; 43 43 $this->_request_data['event']->end = $_POST['end']; 44 if ( $this->_config->get('allow_name_change') 45 && isset($_POST['name'])) 46 { 47 $this->_request_data['event']->name = $_POST['name']; 48 } 44 49 45 50 if ($this->_request_data['master_event']) … … 136 141 $indexer =& $_MIDCOM->get_service('indexer'); 137 142 net_nemein_calendar_viewer::index($this->_controller->datamanager, $indexer, $this->_topic); 138 139 $org_event_name = $data['event']->name;140 141 // Generate URL name142 $data['event']->name = midcom_generate_urlname_from_string($data['event']->title);143 $tries = 0;144 $maxtries = 999;145 while( !$this->_event_name_is_unique($data['event'])146 && $tries < $maxtries)147 {148 $data['event']->name = midcom_generate_urlname_from_string($data['event']->title);149 if ($tries > 0)150 {151 // Append an integer if articles with same name exist152 $data['event']->name .= sprintf("-%03d", $tries);153 }154 $tries++;155 }156 157 if ($data['event']->name != $org_event_name)158 {159 $data['event']->update();160 }161 143 162 144 if ($handler_id != 'create_chooser') … … 196 178 } 197 179 198 function _event_name_is_unique(&$event)199 {200 $qb = net_nemein_calendar_event_dba::new_query_builder();201 202 $qb->add_constraint('node', '=', $this->_request_data['content_topic']->id);203 204 if ($this->_request_data['master_event'])205 {206 $qb->add_constraint('up', '=', $this->_request_data['master_event']);207 }208 209 $qb->add_constraint('name', '=', $event->name);210 211 $results = $qb->count_unchecked();212 213 if ($results === false)214 {215 // QB error216 return false;217 }218 219 if ($results > 0)220 {221 return false;222 }223 224 return true;225 }226 227 180 /** 228 181 * Shows the loaded article.
