Changeset 15584

Show
Ignore:
Timestamp:
03/14/08 11:09:30 (7 months ago)
Author:
rambo
Message:

refactoring and cleaning up the images and image datatypes to fix #226

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/MidCOM_2_8/midcom.helper.datamanager2/type/image.php

    r15492 r15584  
    199199     */ 
    200200    var $output_mode = 'html'; 
     201 
     202    /** 
     203     * Whether this instance is handling single image or multiple images 
     204     * 
     205     * @var string 
     206     * @access private 
     207     */ 
     208    var $_instance_mode = 'single'; 
    201209 
    202210    function _on_initialize() 
     
    972980    function convert_to_storage() 
    973981    { 
    974         // FIXME: better safety for image*s* datatype calling parent::convert_to_storage() 
    975         if (!empty($this->title)) 
     982        if (   $this->_instance_mode === 'single' 
     983            && !empty($this->title)) 
    976984        { 
    977985            foreach ($this->attachments as $identifier => $copy) 
  • branches/MidCOM_2_8/midcom.helper.datamanager2/type/images.php

    r15492 r15584  
    156156 
    157157    /** 
     158     * Titles of each image, keyed by the MD5 identifier 
     159     */ 
     160    var $titles = array(); 
     161 
     162    /** 
    158163     * Output mode 
    159164     * 
     
    170175     */ 
    171176    var $max_count = 0; 
     177 
     178    function _on_initialize() 
     179    { 
     180        $this->_instance_mode = 'multiple'; 
     181        return parent::_on_initialize(); 
     182    } 
    172183 
    173184    /** 
     
    247258        $this->_identifier = $identifier; 
    248259        $this->_title = $title; 
     260        $this->titles[$identifier] = $title; 
    249261        if (array_key_exists($this->_identifier, $this->images)) 
    250262        { 
     
    356368        parent::convert_from_storage($source); 
    357369 
    358         /* 
    359          * TODO: Title handling 
    360         if (array_key_exists('main', $this->attachments)) 
    361         { 
    362             $this->_title = $this->attachments['main']->title; 
    363         } 
    364         */ 
     370        foreach ($this->_attachment_map as $blobs_identifier => $info) 
     371        { 
     372            $images_identifier = $info[0]; 
     373            $this->titles[$images_identifier] = $this->attachments_info[$blobs_identifier]['object']->title; 
     374        } 
    365375    } 
    366376 
     
    370380    function convert_to_storage() 
    371381    { 
    372         /** 
    373          * FIXME: Title handling (now handled separately by the images widget which is incorrect) 
    374         foreach ($this->attachments as $identifier => $copy) 
    375         { 
    376             $this->update_attachment_title($identifier, $this->_title); 
    377         } 
    378         */ 
     382        foreach ($this->titles as $images_identifier => $title) 
     383        { 
     384            if (empty($title)) 
     385            { 
     386                continue; 
     387            } 
     388            if (!isset($this->images[$images_identifier])) 
     389            { 
     390                continue; 
     391            } 
     392            foreach($this->images[$images_identifier] as $key => $info) 
     393            { 
     394                if ($info['object']->title === $title) 
     395                { 
     396                    continue; 
     397                } 
     398                $this->update_attachment_title($info['identifier'], $title); 
     399            } 
     400        } 
    379401 
    380402        return parent::convert_to_storage(); 
  • branches/MidCOM_2_8/midcom.helper.datamanager2/widget/images.php

    r14808 r15584  
    347347        } 
    348348 
     349        $img_title = ''; 
     350        // Some reason we're kicking out-of-sync, check explicitly for POSTed value 
     351        if (   isset($_POST[$this->name]) 
     352            && is_array($_POST[$this->name]) 
     353            && isset($_POST[$this->name]["e_exist_{$identifier}_title"])) 
     354        { 
     355            $img_title = $_POST[$this->name]["e_exist_{$identifier}_title"]; 
     356        } 
     357        // Otherwise use the type title if available 
     358        elseif (isset($this->_type->titles[$identifier])) 
     359        { 
     360            $img_title = $this->_type->titles[$identifier]; 
     361        } 
    349362 
    350363        // Filename column 
     
    356369 
    357370        // Title Column, set the value explicitly, as we are sometimes called after the defaults kick in. 
    358         $html = "<td class='exist title' title='{$info['description']}'>"; 
     371        $html = "<td class='exist title' title='{$img_title}'>"; 
    359372        $this->_elements["s_exist_{$identifier}_title"] =& HTML_QuickForm::createElement('static', "s_exist_{$identifier}_title", '', $html); 
    360373        $attributes = Array 
     
    364377        ); 
    365378        $this->_elements["e_exist_{$identifier}_title"] =& HTML_QuickForm::createElement('text', "e_exist_{$identifier}_title", '', $attributes); 
    366         $this->_elements["e_exist_{$identifier}_title"]->setValue($info['description']); 
     379        $this->_elements["e_exist_{$identifier}_title"]->setValue($img_title); 
    367380 
    368381        if (! $frozen) 
     
    467480        } 
    468481 
    469         if ($this->_elements['e_new_file']->isUploadedFile()) 
    470         { 
    471             $file = $this->_elements['e_new_file']->getValue(); 
    472  
    473             if ( preg_match('/\.(zip|tar(\.gz|\.bz2)?|tgz)$/', strtolower($file['name']), $extension_matches)) 
    474             { 
    475                 // PHP5-TODO: This must be copy-by-value 
    476                 $copy = $file; 
    477                 unset($file); 
    478                 if (! $this->_type->_batch_handler($extension_matches[1], $copy)) 
    479                 { 
    480                     debug_push_class(__CLASS__, __FUNCTION__); 
    481                     debug_add("Failed to add attachments from compressed files to the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
    482                     debug_pop(); 
    483                 } 
    484                 return; 
    485             } 
    486  
    487              
    488             if (   array_key_exists('e_new_title', $values) 
    489                 && !empty($values['e_new_title'])) 
    490             { 
    491                 $title = $values['e_new_title']; 
    492             } 
    493             else 
    494             { 
    495                 $title = $file['name']; 
    496             } 
    497              
    498             if (   array_key_exists('e_new_filename', $values) 
    499                 && !empty($values['e_new_filename'])) 
    500             { 
    501                 $filename = $values['e_new_filename']; 
    502             } 
    503             else 
    504             { 
    505                 $filename = $file['name']; 
    506             } 
    507  
    508             if (! $this->_type->add_image($filename, $file['tmp_name'], $title)) 
     482        if (!$this->_elements['e_new_file']->isUploadedFile()) 
     483        { 
     484            // not uploaded file, abort 
     485            return; 
     486        } 
     487 
     488        $file = $this->_elements['e_new_file']->getValue(); 
     489 
     490        if ( preg_match('/\.(zip|tar(\.gz|\.bz2)?|tgz)$/', strtolower($file['name']), $extension_matches)) 
     491        { 
     492            // PHP5-TODO: This must be copy-by-value 
     493            $copy = $file; 
     494            unset($file); 
     495            if (! $this->_type->_batch_handler($extension_matches[1], $copy)) 
    509496            { 
    510497                debug_push_class(__CLASS__, __FUNCTION__); 
    511                 debug_add("Failed to add an attachment to the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
     498                debug_add("Failed to add attachments from compressed files to the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
    512499                debug_pop(); 
    513500            } 
     501            return; 
     502        } 
     503 
     504         
     505        if (   array_key_exists('e_new_title', $values) 
     506            && !empty($values['e_new_title'])) 
     507        { 
     508            $title = $values['e_new_title']; 
     509        } 
     510        else 
     511        { 
     512            $title = $file['name']; 
     513        } 
     514         
     515        if (   array_key_exists('e_new_filename', $values) 
     516            && !empty($values['e_new_filename'])) 
     517        { 
     518            $filename = $values['e_new_filename']; 
     519        } 
     520        else 
     521        { 
     522            $filename = $file['name']; 
     523        } 
     524 
     525        if (! $this->_type->add_image($filename, $file['tmp_name'], $title)) 
     526        { 
     527            debug_push_class(__CLASS__, __FUNCTION__); 
     528            debug_add("Failed to add an attachment to the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
     529            debug_pop(); 
    514530        } 
    515531    } 
     
    520536     * 1. If the delete button was clicked, the image is dropped. 
    521537     * 2. If a new file has been uploaded, it replaces the current one. 
    522      * 3. If neither of the above is triggered, the title of the image is synchronized. 
    523538     * 
    524539     * Calls for images which are not listed in the form, will be silently ignored. 
     
    538553        } 
    539554 
    540         if (array_key_exists("{$this->name}_e_exist_{$identifier}_delete", $values)) 
    541         { 
    542             if (! $this->_type->delete_image($identifier)) 
    543             { 
    544                 debug_push_class(__CLASS__, __FUNCTION__); 
    545                 debug_add("Failed to delete the image {$identifier} on the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
    546                 debug_pop(); 
    547             } 
    548         } 
    549         else if 
    550         (   array_key_exists("e_exist_{$identifier}_file", $this->_elements) 
    551                  && $this->_elements["e_exist_{$identifier}_file"]->isUploadedFile()) 
    552         { 
    553             $file = $this->_elements["e_exist_{$identifier}_file"]->getValue(); 
    554             $title = $values["e_exist_{$identifier}_title"]; 
    555             $filename = $this->_type->images[$identifier]['main']['filename']; 
    556  
    557             if (! $title) 
    558             { 
    559                 $title = $filename; 
    560             } 
    561  
    562             if (! $this->_type->update_image($identifier, $filename, $file['tmp_name'], $title)) 
    563             { 
    564                 debug_push_class(__CLASS__, __FUNCTION__); 
    565                 debug_add("Failed to update the image {$identifier} on the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
    566                 debug_pop(); 
    567             } 
    568         } 
    569         else if 
    570         (   array_key_exists("e_exist_{$identifier}_title", $values) 
    571                  && isset($this->_type->images[$identifier]['main']) 
    572                  && isset($this->_type->images[$identifier]['main']['description']) 
    573                  && $values["e_exist_{$identifier}_title"] != $this->_type->images[$identifier]['main']['description']) 
    574         { 
    575             $this->_type->update_image_title($identifier, $values["e_exist_{$identifier}_title"]); 
     555        switch(true) 
     556        { 
     557            // Image to be deleted 
     558            case (array_key_exists("{$this->name}_e_exist_{$identifier}_delete", $values)): 
     559                if (! $this->_type->delete_image($identifier)) 
     560                { 
     561                    debug_push_class(__CLASS__, __FUNCTION__); 
     562                    debug_add("Failed to delete the image {$identifier} on the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
     563                    debug_pop(); 
     564                } 
     565                break; 
     566            // Image to be updated 
     567            case (   array_key_exists("e_exist_{$identifier}_file", $this->_elements) 
     568                  && $this->_elements["e_exist_{$identifier}_file"]->isUploadedFile()): 
     569                $file = $this->_elements["e_exist_{$identifier}_file"]->getValue(); 
     570                $title = $values["e_exist_{$identifier}_title"]; 
     571                $filename = $this->_type->images[$identifier]['main']['filename']; 
     572     
     573                if (! $title) 
     574                { 
     575                    $title = $filename; 
     576                } 
     577     
     578                if (! $this->_type->update_image($identifier, $filename, $file['tmp_name'], $title)) 
     579                { 
     580                    debug_push_class(__CLASS__, __FUNCTION__); 
     581                    debug_add("Failed to update the image {$identifier} on the field '{$this->name}'. Ignoring silently.", MIDCOM_LOG_WARN); 
     582                    debug_pop(); 
     583                } 
     584                break; 
    576585        } 
    577586    } 
     
    629638     * Nothing to do here. 
    630639     */ 
    631     function sync_type_with_widget($results) {} 
     640    function sync_type_with_widget($results) 
     641    { 
     642        $values = $results[$this->name]; 
     643 
     644        foreach ($this->_type->images as $identifier => $info) 
     645        { 
     646            if (!isset($values["e_exist_{$identifier}_title"])) 
     647            { 
     648                continue; 
     649            } 
     650            $this->_type->titles[$identifier] = $values["e_exist_{$identifier}_title"]; 
     651        } 
     652    } 
    632653 
    633654    /** 
     
    639660        foreach($this->_type->images as $identifier => $images) 
    640661        { 
    641             if (   isset($images['main']) 
    642                 && isset($images['main']['description'])) 
     662            if (isset($this->_type->titles[$identifier])) 
    643663            { 
    644                 $defaults["e_exist_{$identifier}_title"] = $images['main']['description']; 
     664                $defaults["e_exist_{$identifier}_title"] = $this->_type->titles[$identifier]; 
    645665            } 
    646666            else