public function saveInto(DataObjectInterface $record)
 {
     if ($this->name) {
         $value = $this->dataValue();
         $file = null;
         if ($value['id']) {
             $file = CloudinaryFile::get()->byID($value['id']);
         }
         if (!$file) {
             $file = new CloudinaryFile();
         }
         if ($value['resource_type'] == 'image') {
             $file->ClassName = 'CloudinaryImage';
         }
         if ($value['url']) {
             $file->update(array('CloudinaryURL' => $value['url'], 'Title' => $value['title'], 'Size' => $value['size'], 'FileName' => $value['filename'], 'ResourceType' => $value['resource_type'], 'Height' => (int) $value['height'], 'Width' => (int) $value['width'], 'Format' => $value['format']));
             $file->write();
             $record->setCastedField($this->name . 'ID', $file->ID);
         } else {
             if ($file->exists()) {
                 $file->delete();
             }
             $record->setCastedField($this->name . 'ID', 0);
         }
     }
 }
 /**
  * Scrape the content of a field to detect anly links to local SiteTree pages or files
  *
  * @param string $field The name of the field on {@link @owner} to scrape
  */
 public function trackLinksInField($field)
 {
     $record = $this->owner;
     $linkedCloudinary = array();
     preg_match_all('/\\[cloudinary,id=\\"\\d*\\"/i', $record->{$field}, $matches);
     if ($matches && isset($matches[0])) {
         foreach ($matches[0] as $match) {
             $id = str_replace('"', '', str_replace('[cloudinary,id=', '', $match));
             if ($file = CloudinaryFile::get()->byID((int) $id)) {
                 $linkedCloudinary[] = (int) $id;
             } else {
                 $record->HasBrokenFile = true;
             }
         }
     }
     // Update the "CloudinaryTracking" many_many
     if ($record->ID && $record->many_many('CloudinaryTracking') && ($tracker = $record->CloudinaryTracking())) {
         $tracker->removeByFilter(sprintf('"FieldName" = \'%s\' AND "%s" = %d', $field, $tracker->getForeignKey(), $record->ID));
         if ($linkedCloudinary) {
             foreach ($linkedCloudinary as $item) {
                 $tracker->add($item, array('FieldName' => $field));
             }
         }
     }
     parent::trackLinksInField($field);
 }
 /**
  * @param $arguments
  * @param null $content
  * @param null $parser
  * @return string
  *
  * Parse short codes for the cloudinary tags
  */
 public static function cloudinary_markdown($arguments, $content = null, $parser = null)
 {
     if (!isset($arguments['id']) || !is_numeric($arguments['id'])) {
         return;
     }
     $file = CloudinaryFile::get()->byID($arguments['id']);
     if ($file) {
         if ($file->ClassName == 'CloudinaryImage') {
             $alt = "";
             if (isset($arguments['alt'])) {
                 $alt = $arguments['alt'];
             }
             if (isset($arguments['width']) && isset($arguments['height'])) {
                 return $file->customise(array('width' => $arguments['width'], 'height' => $arguments['height'], 'alt' => $alt))->renderWith('MarkDownShortCode');
             } else {
                 return $file->customise(array('alt' => $alt))->renderWith('MarkDownShortCode');
             }
         }
     }
 }
 /**
  * @return mixed
  */
 public function ColorSelectThumbnail()
 {
     if ($video = CloudinaryFile::get()->byID($this->value)) {
         return $video->GetFileImage(200, 112, 90);
     }
 }
 /**
  * @return DataObject|File
  */
 public function getItem()
 {
     return CloudinaryFile::get()->byId($this->itemID);
 }