Changeset 16375

Show
Ignore:
Timestamp:
05/14/08 11:21:48 (5 months ago)
Author:
rambo
Message:

forward port r16374

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/net.nemein.calendar/config/config.inc

    r15555 r16375  
    3434), 
    3535 
     36 
    3637'language' => null, 
     38'allow_name_change' => false, // WARNING: Enabling this enables you to shoot your own foot 
  • trunk/midcom/net.nemein.calendar/event.php

    r16252 r16375  
    1515class net_nemein_calendar_event_dba extends __net_nemein_calendar_event_dba 
    1616{ 
     17    var $_config = false; 
     18 
    1719    function __construct($guid = null)  
    1820    { 
    19         return parent::__construct($guid); 
     21        $stat = parent::__construct($guid); 
     22        $interface =& $_MIDCOM->componentloader->get_interface_class('net.nemein.calendar'); 
     23        $this->_config = $interface->get_config_for_topic(); 
     24        return $stat; 
    2025    } 
    2126 
     
    139144    } 
    140145 
     146    /** 
     147     * Checks that the event time range is sane 
     148     * @return bool indicating sanity 
     149     */ 
    141150    function _check_time_range() 
    142151    { 
     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        } 
    143179        if (   $this->start === '0000-00-00 00:00:00' 
    144180            && $this->end === '0000-00-00 00:00:00') 
     
    151187        if ($end_comparable < $start_comparable) 
    152188        { 
     189            debug_push_class(__CLASS__, __FUNCTION__); 
     190            debug_add("Cannot end before starting ({$end_comparable} < {$start_comparable})", MIDCOM_LOG_ERROR); 
     191            debug_pop(); 
    153192            mgd_set_errno(MGD_ERR_RANGE); 
    154193            return false; 
    155194        } 
    156195        // Avoid problems with events too close to the epoch (highly unlikely usage scenario in any case) 
    157         $epoch = '19720102000000'; 
     196        static $epoch = '19720102000000'; 
    158197        if (   $start_comparable < $epoch 
    159198            || $end_comparable < $epoch) 
    160199        { 
     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(); 
    161203            mgd_set_errno(MGD_ERR_RANGE); 
    162204            return false; 
     
    165207    } 
    166208 
     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 
    167250    function _on_creating() 
    168251    { 
    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; 
    170266    } 
    171267 
    172268    function _on_updating() 
    173269    { 
    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; 
    175291    } 
    176292 
     
    180296        { 
    181297            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__); 
    182324            debug_add("Detected _on_updated loop on #{$this->id}", MIDCOM_LOG_ERROR); 
    183325            debug_pop(); 
     
    185327        else 
    186328        { 
    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         else 
    203         { 
    204329            $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            } 
    206340            unset($GLOBALS['net_nemein_calendar_event_dba__on_updated_loop_{$this->guid}']); 
    207341        } 
  • trunk/midcom/net.nemein.calendar/handler/create.php

    r16252 r16375  
    4242        $this->_request_data['event']->start = $_POST['start']; 
    4343        $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        } 
    4449 
    4550        if ($this->_request_data['master_event']) 
     
    136141                $indexer =& $_MIDCOM->get_service('indexer'); 
    137142                net_nemein_calendar_viewer::index($this->_controller->datamanager, $indexer, $this->_topic); 
    138  
    139                 // Generate URL name 
    140                 if ($data['event']->name == '') 
    141                 { 
    142                     $data['event']->name = midcom_generate_urlname_from_string($data['event']->title); 
    143                     $tries = 0; 
    144                     $maxtries = 999; 
    145                     while(   !$data['event']->update() 
    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 exist 
    152                             $data['event']->name .= sprintf("-%03d", $tries); 
    153                         } 
    154                         $tries++; 
    155                     } 
    156                 } 
    157143 
    158144                if ($handler_id != 'create_chooser') 
     
    192178    } 
    193179 
    194  
    195180    /** 
    196181     * Shows the loaded article.