[TYPO3-mvc] adding files through a service: references not updated

Henjo Hoeksma me at henjohoeksma.nl
Fri Feb 25 22:30:03 CET 2011


Hi list,

I am working on a gallery extension that reads images from a path.

I have two issues:
1/  The images are not uploaded to the uploads folder (hence no preview 
images are shown in the BE) (not my biggest concern, but it would be 
nice to have).
2/ References are not updated on deleting or adding this way... (this 
leaves clutter in the db, which is of concern to me)

Hope you can provide me with some suggestions.

Service code:
<code>
class Tx_Gallery_Services_LoadFromPathService extends 
Tx_Gallery_Domain_Model_Gallery {

    /**
     * @param Tx_Gallery_Domain_Model_Gallery $gallery
     * @return void
     */
    public function 
updateImagesFromPath(Tx_Gallery_Domain_Model_Gallery $gallery) {

        // Get images from path
        $path = $gallery->getPath();
        $files = array();
        if ($handle = opendir($path)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $files[]= $file;
                }
            }
            closedir($handle);
        }

        // Get current images from the gallery
        $currentImages = $gallery->getImages();
        $currentImagesArray = array();
        foreach ($currentImages as $v) {
            $currentImagesArray[] = $v->getFile();
        }

        $filesToRemove = array_diff($currentImagesArray,$files);
        $filesToAdd = array_diff($files,$currentImagesArray);

        for($i=0;count($filesToRemove)>$i;$i++){
            foreach($currentImages as $v){
                if(in_array($v->getFile(),$filesToRemove)){
                    $gallery->removeImage($v);
                }
            }
        }

        for($i=0;count($filesToAdd)>$i;$i++){
            $image = new Tx_Gallery_Domain_Model_Image;
            $image->setFile($filesToAdd[$i]);
            $gallery->addImage($image);
            unset($image);
        }


    }
}
</code>

Thanks for your response.



More information about the TYPO3-project-typo3v4mvc mailing list