Version: current

      1 <?php
      2
      3 class ImageDataObjectManager extends FileDataObjectManager {
      4     protected static $sliderWidth = 150;
      5     protected static $minImageSize = 25;
      6     protected static $maxImageSize = 300;
      7
      8
      9     public $view = "grid";
     10     protected $limitFileTypes = array ('jpg','jpeg','gif','png');
     11     public $template = "ImageDataObjectManager";
     12     public $itemClass = "ImageDataObjectManager_Item";
     13     public $popupClass = "ImageDataObjectManager_Popup";
     14     public $importClass = "Image";
     15
     16     public $imageSize = 100;
     17
     18     public function __construct($controller, $name = null, $sourceClass = null, $fileFieldName = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
     19         parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
     20         Requirements::css('dataobject_manager/css/ui/dom_jquery_ui.css');
     21         Requirements::javascript('dataobject_manager/javascript/imagedataobject_manager.js');
     22
     23         if(isset($_REQUEST['ctf'][$this->Name()])) {
     24             $this->imageSize = $_REQUEST['ctf'][$this->Name()]['imagesize'];
     25         }
     26         $this->setAllowedFileTypes($this->limitFileTypes);
     27
     28         if (property_exists('WebylonSiteConfig', 'max_image_upload_size')) {
     29             $this->uploadMaxSize = WebylonSiteConfig::$max_image_upload_size;
     30         } else {
     31             $this->uploadMaxSize = ((int)ini_get('upload_max_filesize')) * 1024 * 1024;
     32         }
     33     }
     34
     35     function handleItem($request) {
     36         return new ImageDataObjectManager_ItemRequest($this, $request->param('ID'));
     37     }
     38
     39     public function getQueryString($params = array()) {
     40         $imagesize = isset($params['imagesize'])? $params['imagesize'] : $this->imageSize;
     41         return parent::getQueryString($params)."&ctf[{$this->Name()}][imagesize]={$imagesize}";
     42     }
     43
     44     public function SliderPercentage() {
     45         return ($this->imageSize - self::$minImageSize) / ((self::$maxImageSize - self::$minImageSize) / 100);
     46     }
     47
     48     public function SliderPosition() {
     49         return floor(($this->SliderPercentage()/100) * self::$sliderWidth) - (.17 * $this->SliderPercentage()); // handle is 16px wide
     50     }
     51
     52
     53 }
     54
     55 class ImageDataObjectManager_Item extends FileDataObjectManager_Item {
     56
     57     function __construct(DataObject $item, ComplexTableField $parent) {
     58         parent::__construct($item, $parent);
     59     }
     60
     61     public function FileIcon() {
     62         $file = ($this->parent->hasDataObject) ? $this->obj($this->parent->fileFieldName) : $this->item;
     63         if($file) {
     64             if($this->parent->imageSize <= 50) $size = 50;
     65             elseif($this->parent->imageSize <= 100) $size = 100;
     66             elseif($this->parent->imageSize <= 200) $size = 200;
     67             else $size = 300;
     68             return ($file instanceof Image && $cropped = $file->CroppedImage($size, $size)) ? $cropped->URL : $file->Icon();
     69         }
     70         return false;
     71     }
     72
     73     public function ImageSize() {
     74         return $this->parent->imageSize;
     75     }
     76
     77 }
     78
     79 class ImageDataObjectManager_Popup extends FileDataObjectManager_Popup {
     80     function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
     81         parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
     82         Requirements::css('dataobject_manager/css/imagedataobject_manager.css');
     83     }
     84
     85 }
     86
     87 class ImageDataObjectManager_ItemRequest extends FileDataObjectManager_ItemRequest {
     88     function __construct($ctf, $itemID) {
     89         parent::__construct($ctf, $itemID);
     90     }
     91
     92     function DetailForm($childID = null) {
     93         if($this->ctf->hasDataObject) {
     94             $fileField = $this->ctf->fileFieldName;
     95             $imgObj = $this->dataObj()->$fileField();
     96         } else {
     97             $imgObj = $this->dataObj();
     98         }
     99
    100         $form = parent::DetailForm($childID);
    101
    102         $form->Fields()->insertAfter($this->ctf->getPreviewFieldFor($imgObj, 200), 'open');
    103         return $form;
    104     }
    105 }
    106
    107 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -