Changeset 16485

Show
Ignore:
Timestamp:
05/28/08 14:05:48 (5 months ago)
Author:
adrenalin
Message:

Added locking feature to prevent simultaneous editing of an object.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.helper.datamanager2/config/config.inc

    r16421 r16485  
    126126), 
    127127'fck_default_config_snippet' => 'file://midcom/helper/datamanager2/config/fckeditor_default.inc', 
     128 
     129// Lock timeout, which defines how many seconds the object should be locked 
     130'lock_timeout' => 1800, 
  • trunk/midcom/midcom.helper.datamanager2/config/manifest.inc

    r15038 r16485  
    33'state' => 'beta', 
    44'purecode' => true, 
     5'privileges' => array 
     6( 
     7    'break_lock' => 'midcom.helper.datamanager2:break_lock', 
     8), 
    59'package.xml' => array 
    610( 
  • trunk/midcom/midcom.helper.datamanager2/controller.php

    r14381 r16485  
    6161    var $formmanager = null; 
    6262 
     63    /** 
     64     * Lock timeout defines the length of lock in seconds. 
     65     *  
     66     * @access public 
     67     * @var integer 
     68     */ 
     69    var $lock_timeout = null; 
    6370 
    6471    /** 
     
    6976         $this->_component = 'midcom.helper.datamanager2'; 
    7077         parent::midcom_baseclasses_components_purecode(); 
    71  
    7278    } 
    7379 
     
    7985    function initialize() 
    8086    { 
     87        if (is_null($this->lock_timeout)) 
     88        { 
     89            $this->lock_timeout = (int) $this->_config->get('lock_timeout'); 
     90        } 
     91         
    8192        return true; 
    8293    } 
     
    243254    function display_form() 
    244255    { 
     256        if (midcom_helper_datamanager2_controller::is_locked($this->datamanager->storage->object, $this->lock_timeout)) 
     257        { 
     258            $this->show_remove_lock(); 
     259            return; 
     260        } 
     261         
    245262        $this->formmanager->display_form(); 
    246263    } 
     264     
     265    /** 
     266     * Show the lock status 
     267     *  
     268     * @access public 
     269     */ 
     270    function show_remove_lock() 
     271    { 
     272        if (   function_exists('mgd_is_element_loaded') 
     273            && mgd_is_element_loaded('midcom_helper_datamanager2_remove_lock')) 
     274        { 
     275            mgd_show_element('midcom_helper_datamanager2_remove_lock'); 
     276        } 
     277        else 
     278        { 
     279            $user = $this->datamanager->storage->object->get_parameter('midcom.helper.datamanager2.lock', 'user'); 
     280            $expires = strtotime($this->datamanager->storage->object->get_parameter('midcom.helper.datamanager2.lock', 'expires')); 
     281             
     282            $person = new midcom_db_person($user); 
     283            ?> 
     284                <div class="midcom_helper_datamanager2_remove_lock"> 
     285                    <h2><?php echo $this->_l10n->get('object locked'); ?></h2> 
     286                    <p> 
     287                        <?php echo sprintf($this->_l10n->get('this object was locked by %s'), $person->name); ?>. 
     288                        <?php echo sprintf($this->_l10n->get('lock will expire on %s'), strftime('%x %X', $expires)); ?>. 
     289                    </p> 
     290            <?php 
     291            if ($_MIDCOM->auth->can_user_do('midcom.helper.datamanager2:break_lock', null, 'midcom_helper_datamanager2_controller', 'midcom.helper.datamanager2')) 
     292            { 
     293                echo "<form method=\"post\">\n"; 
     294                echo "    <p class=\"break_lock\">\n"; 
     295                echo "        <input type=\"hidden\" name=\"midcom_helper_datamanager2_object\" value=\"{$this->datamanager->storage->object->guid}\" />\n"; 
     296                echo "        <input type=\"submit\" name=\"midcom_helper_datamanager2_remove_lock\" value=\"" . $this->_l10n->get('break the lock') . "\" class=\"break_lock\" />\n"; 
     297                echo "    </p>\n"; 
     298                echo "</form>\n"; 
     299            } 
     300            ?> 
     301                </div> 
     302            <?php 
     303        } 
     304    } 
     305     
     306    /** 
     307     * Check if the object is being edited elsewhere 
     308     *  
     309     * @static 
     310     * @access public 
     311     * @param mixed $guid      Object or GUID of the object 
     312     * @return boolean         True if the object is locked 
     313     */ 
     314    function is_locked($target, $timeout = 0) 
     315    { 
     316        // Remove the object lock if applicable 
     317        if (   isset($_REQUEST['midcom_helper_datamanager2_remove_lock']) 
     318            && isset($_REQUEST['midcom_helper_datamanager2_object'])) 
     319        { 
     320            // Get the object 
     321            $object = $_MIDCOM->dbfactory->get_object_by_guid($_POST['midcom_helper_datamanager2_object']); 
     322             
     323            // Remove the lock, if permission is granted 
     324            if ($_MIDCOM->auth->can_user_do('midcom.helper.datamanager2:break_lock', null, 'midcom_helper_datamanager2_controller', 'midcom.helper.datamanager2')) 
     325            { 
     326                midcom_helper_datamanager2_controller::set_lock($object, $timeout); 
     327            } 
     328            else 
     329            { 
     330                $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.helper.datamanager2', 'midcom.helper.datamanager2'), $_MIDCOM->i18n->get_string('permission denied', 'midcom'), 'error'); 
     331            } 
     332        } 
     333         
     334        // Check that we have an object at disposal 
     335        if (is_object($target)) 
     336        { 
     337            $object =& $target; 
     338        } 
     339        else 
     340        { 
     341            $object = $_MIDCOM->dbfactory->get_object_by_guid($target); 
     342        } 
     343         
     344        // Couldn't get the object. Different error handling here? Controller should have already alarmed the user, though... 
     345        if (   !$object 
     346            || !$object->guid) 
     347        { 
     348            return false; 
     349        } 
     350         
     351        $expires = (int) strtotime($object->get_parameter('midcom.helper.datamanager2.lock', 'expires')); 
     352         
     353        // Object not locked, allow editing 
     354        if (!$expires) 
     355        { 
     356            return false; 
     357        } 
     358         
     359        // Get the person who locked the object 
     360        $user = $object->get_parameter('midcom.helper.datamanager2.lock', 'user'); 
     361         
     362        // Lock was created by the user, allow unquestionable editing 
     363        if (   isset($_MIDCOM->auth->user) 
     364            && isset($_MIDCOM->auth->user->guid) 
     365            && $user === $_MIDCOM->auth->user->guid) 
     366        { 
     367            return false; 
     368        } 
     369         
     370        // Object lock is no longer valid 
     371        if (time() > $expires) 
     372        { 
     373            return false; 
     374        } 
     375         
     376        // Lock checked, object locked 
     377        return true; 
     378    } 
     379     
     380    /** 
     381     * Set the object lock 
     382     *  
     383     * @static 
     384     * @access public 
     385     * @param mixed $object    Object that should be locked 
     386     * @param int $timeout     Length of the lock timeout 
     387     */ 
     388    function set_lock($object, $timeout) 
     389    { 
     390        if ((int) $timeout < 1) 
     391        { 
     392            $object->set_parameter('midcom.helper.datamanager2.lock', 'user', ''); 
     393            $object->set_parameter('midcom.helper.datamanager2.lock', 'expires', ''); 
     394            return true; 
     395        } 
     396         
     397        $object->set_parameter('midcom.helper.datamanager2.lock', 'user', $_MIDCOM->auth->user->guid); 
     398        $object->set_parameter('midcom.helper.datamanager2.lock', 'expires', strftime('%Y-%m-%d %H:%M:%S', time() + $timeout)); 
     399         
     400        return true; 
     401    } 
    247402} 
     403?> 
  • trunk/midcom/midcom.helper.datamanager2/controller/ajax.php

    r16373 r16485  
    2828     
    2929    /** 
    30      * AJAX controller initialization. Loads required Javascript libraries 
     30     * AJAX controller initialization. Loads required Javascript libraries and connects to the parent class initialization. 
    3131     * 
    3232     * @return boolean Indicating success. 
     
    3434    function initialize() 
    3535    { 
     36        parent::initialize(); 
     37         
    3638        if (count($this->schemadb) == 0) 
    3739        { 
     
    286288        $result = $this->formmanager->process_form(); 
    287289 
     290        // Remove the lock 
     291        if (   $result === 'save' 
     292            || $result === 'cancel') 
     293        { 
     294            midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, 0); 
     295        } 
     296        // or set it, if needed 
     297        elseif (!midcom_helper_datamanager2_controller::is_locked($this->datamanager->storage->object, $this->lock_timeout)) 
     298        { 
     299            midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, $this->lock_timeout); 
     300        } 
     301         
    288302        // Handle successful save explicitly. 
    289303        if (   $result == 'save' 
     
    326340        return $result; 
    327341    } 
    328  
    329342} 
    330  
    331343?> 
  • trunk/midcom/midcom.helper.datamanager2/controller/simple.php

    r16373 r16485  
    2323{ 
    2424    /** 
    25      * Empty default implementation, this calls won't do much. 
     25     * Check if a schema has been set 
    2626     * 
    2727     * @return boolean Indicating success. 
     
    2929    function initialize() 
    3030    { 
     31        parent::initialize(); 
     32         
    3133        if (count($this->schemadb) == 0) 
    3234        { 
     
    8688        $result = $this->formmanager->process_form(); 
    8789         
     90        // Remove the lock 
     91        if (   $result === 'save' 
     92            || $result === 'cancel') 
     93        { 
     94            midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, 0); 
     95        } 
     96        // or set it, if needed 
     97        elseif (!midcom_helper_datamanager2_controller::is_locked($this->datamanager->storage->object, $this->lock_timeout)) 
     98        { 
     99            midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, $this->lock_timeout); 
     100        } 
     101         
    88102        // Handle successful save explicitly. 
    89103        if (   $result == 'save' 
     
    131145        return $result; 
    132146    } 
    133  
    134147} 
    135  
    136148?> 
  • trunk/midcom/midcom.helper.datamanager2/documentation/CHANGES

    r14891 r16485  
    88  which might even break existing functionality. 
    99- All items marked with "+" represent completely new features. 
     10 
     112008-05-28 adrenalin 
     12  + Added locking feature for disabling simultaneous editing 
    1013 
    11142008-02-12 adrenalin 
  • trunk/midcom/midcom.helper.datamanager2/formmanager.php

    r16300 r16485  
    10001000    function get_clicked_button() 
    10011001    { 
    1002         if (array_key_exists('midcom_helper_datamanager2_save', $_REQUEST)) 
    1003         { 
    1004             return 'save'; 
    1005         } 
    1006         else if (array_key_exists('midcom_helper_datamanager2_next', $_REQUEST)) 
    1007         { 
    1008             return 'next'; 
    1009         } 
    1010         else if (array_key_exists('midcom_helper_datamanager2_previous', $_REQUEST)) 
    1011         { 
    1012             return 'previous'; 
    1013         } 
    1014         else if (array_key_exists('midcom_helper_datamanager2_cancel', $_REQUEST)) 
    1015         { 
    1016             return 'cancel'; 
    1017         } 
    1018         else if (array_key_exists('midcom_helper_datamanager2_preview', $_REQUEST)) 
    1019         { 
    1020             return 'preview'; 
    1021         } 
    1022         else 
    1023         { 
    1024             return 'edit'; 
    1025         } 
    1026     } 
    1027  
     1002        switch (true) 
     1003        { 
     1004            case (array_key_exists('midcom_helper_datamanager2_save', $_REQUEST)): 
     1005                return 'save'; 
     1006             
     1007            case (array_key_exists('midcom_helper_datamanager2_next', $_REQUEST)): 
     1008                return 'next'; 
     1009                 
     1010            case (array_key_exists('midcom_helper_datamanager2_previous', $_REQUEST)): 
     1011                return 'previous'; 
     1012                 
     1013            case (array_key_exists('midcom_helper_datamanager2_cancel', $_REQUEST)): 
     1014                return 'cancel'; 
     1015                 
     1016            case (array_key_exists('midcom_helper_datamanager2_preview', $_REQUEST)): 
     1017                return 'preview'; 
     1018                 
     1019            default: 
     1020                return 'edit'; 
     1021        } 
     1022    } 
    10281023} 
     1024?> 
  • trunk/midcom/midcom.helper.datamanager2/locale/default.en.txt

    r15960 r16485  
    1616---STRINGEND 
    1717 
     18---STRING add new file 
     19Add a new file 
     20---STRINGEND 
     21 
    1822---STRING archived image 
    1923Archived image 
     
    5357---STRINGEND 
    5458 
     59---STRING drag and drop to sort 
     60Drag and drop to sort 
     61---STRINGEND 
     62 
    5563---STRING field %s is required 
    5664The field %s is required, please fill it out. 
     
    7381---STRINGEND 
    7482 
     83---STRING lock will expire on %s 
     84Lock will expire on %s 
     85---STRINGEND 
     86 
    7587---STRING midcom.helper.datamanager2 
    7688Data management and form rendering library 
     
    8193---STRINGEND 
    8294 
     95---STRING object locked 
     96Object locked 
     97---STRINGEND 
     98 
    8399---STRING passwords do not match 
    84100The passwords do not match. 
     
    101117---STRINGEND 
    102118 
     119---STRING this object was locked by %s 
     120This object was locked by %s 
     121---STRINGEND 
     122 
    103123---STRING type blobs: file size 
    104124File size 
     
    117137---STRINGEND 
    118138 
     139---STRING type number: value must not be larger then %s 
     140The value of this field may not be larger then %s. 
     141---STRINGEND 
     142 
     143---STRING type number: value must not be smaller then %s 
     144The value of this field may not be smaller then %s. 
     145---STRINGEND 
     146 
    119147---STRING type php: parse error in line %s 
    120148Parse Error in line %s 
    121149---STRINGEND 
    122150 
    123 ---STRING type number: value must not be larger then %s 
    124 The value of this field may not be larger then %s. 
    125 ---STRINGEND 
    126  
    127 ---STRING type number: value must not be smaller then %s 
    128 The value of this field may not be smaller then %s. 
    129 ---STRINGEND 
    130  
    131151---STRING type select: multiselect not allowed 
    132152No multiple selection allowed for this field. 
     
    153173---STRINGEND 
    154174 
    155 ---STRING add new file 
    156 Add a new file 
    157 ---STRINGEND 
    158  
    159175---STRING upload image 
    160176Upload image 
     
    221237---STRINGEND 
    222238 
    223 ---STRING drag and drop to sort 
    224 Drag and drop to sort 
    225 ---STRINGEND 
    226  
     239---STRING break the lock 
     240Break the lock 
     241---STRINGEND 
     242 
  • trunk/midcom/midcom.helper.datamanager2/locale/default.fi.txt

    r14715 r16485  
    1616---STRINGEND 
    1717 
     18---STRING add new file 
     19 
     20---STRINGEND 
     21 
    1822---STRING archived image 
    1923Arkistoitu kuva 
     
    5256---STRINGEND 
    5357 
     58---STRING drag and drop to sort 
     59JÀrjestele hiirellÀ raahaamalla 
     60---STRINGEND 
     61 
    5462---STRING field %s is required 
    5563KenttÀ %s on pakollinen, ole hyvÀ ja tÀytÀ se. 
     
    7280---STRINGEND 
    7381 
     82---STRING lock will expire on %s 
     83Lukko avautuu ajalla %s 
     84---STRINGEND 
     85 
    7486---STRING midcom.helper.datamanager2 
    7587Tiedonhallinta- ja lomakekirjasto 
     
    8092---STRINGEND 
    8193 
     94---STRING object locked 
     95Kohde lukittu 
     96---STRINGEND 
     97 
    8298---STRING passwords do not match 
    8399Salasanat eivÀt tÀsmÀÀ 
     
    100116---STRINGEND 
    101117 
     118---STRING this object was locked by %s 
     119TÀmÀn kohteen lukitsi %s 
     120---STRINGEND 
     121 
    102122---STRING type blobs: file size 
    103123Tiedoston koko 
     
    124144---STRINGEND 
    125145 
     146---STRING type php: parse error in line %s 
     147 
     148---STRINGEND 
     149 
    126150---STRING type select: multiselect not allowed 
    127151Monivalinta ei ole sallittu 
     
    156180---STRINGEND 
    157181 
     182---STRING validation failed: date 
     183 
     184---STRINGEND 
     185 
    158186---STRING validation failed: email 
    159187TÀmÀ ei ole oikea sÀhköpostiosoite 
     
    208236---STRINGEND 
    209237 
    210 ---STRING drag and drop to sort 
    211 JÀrjestele hiirellÀ raahaamalla 
    212 ---STRINGEND 
    213  
     238---STRING break the lock 
     239Murra lukko 
     240---STRINGEND 
     241