public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $quickTags = FlickrTag::CreateOrFindTags($this->QuickTags);
     $this->FlickrTags()->addMany($quickTags);
     if ($this->ID && $this->FlickrPhotos()->count() > 0) {
         if ($this->Title == '') {
             $this->Title = $this->FlickrPhotos()->first()->TakenAt . ' - ' . $this->FlickrPhotos()->last()->TakenAt;
         }
     } else {
         $this->Virginal = true;
     }
 }
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $quickTags = FlickrTag::CreateOrFindTags($this->QuickTags);
     $this->FlickrTags()->addMany($quickTags);
     if ($this->LargeWidth > 0) {
         $this->AspectRatio = $this->LargeHeight / $this->LargeWidth;
     }
     if (!$this->KeepClean) {
         $this->IsDirty = true;
     } else {
         $this->IsDirty = false;
     }
 }
 public static function CreateOrFindTags($csv)
 {
     $result = new ArrayList();
     if (trim($csv) == '') {
         return $result;
         // ie empty array
     }
     $tags = explode(',', $csv);
     foreach ($tags as $tagName) {
         $tagName = trim($tagName);
         if (!$tagName) {
             continue;
         }
         $ftag = DataList::create('FlickrTag')->where("Value='" . strtolower($tagName) . "'")->first();
         if (!$ftag) {
             $ftag = FlickrTag::create();
             $ftag->RawValue = $tagName;
             $ftag->Value = strtolower($tagName);
             $ftag->write();
         }
         $result->add($ftag);
     }
     return $result;
 }
 private function createFromFlickrArray($value, $only_new_photos = false)
 {
     gc_collect_cycles();
     $flickrPhotoID = $value['id'];
     // the author, e.g. gordonbanderson
     $pathalias = $value['pathalias'];
     // do we have a set object or not
     $flickrPhoto = DataObject::get_one('FlickrPhoto', 'FlickrID=' . $flickrPhotoID);
     // if a set exists update data, otherwise create
     if (!$flickrPhoto) {
         $flickrPhoto = new FlickrPhoto();
     } else {
         if ($only_new_photos) {
             continue;
         }
     }
     $flickrPhoto->Title = $value['title'];
     $flickrPhoto->FlickrID = $flickrPhotoID;
     $flickrPhoto->KeepClean = true;
     $flickrPhoto->MediumURL = $value['url_m'];
     $flickrPhoto->MediumHeight = $value['height_m'];
     $flickrPhoto->MediumWidth = $value['width_m'];
     $flickrPhoto->SquareURL = $value['url_s'];
     $flickrPhoto->SquareHeight = $value['height_s'];
     $flickrPhoto->SquareWidth = $value['width_s'];
     $flickrPhoto->ThumbnailURL = $value['url_t'];
     $flickrPhoto->ThumbnailHeight = $value['height_t'];
     $flickrPhoto->ThumbnailWidth = $value['width_t'];
     $flickrPhoto->SmallURL = $value['url_s'];
     $flickrPhoto->SmallHeight = $value['height_s'];
     $flickrPhoto->SmallWidth = $value['width_s'];
     // If the image is too small, large size will not be set
     if (isset($value['url_l'])) {
         $flickrPhoto->LargeURL = $value['url_l'];
         $flickrPhoto->LargeHeight = $value['height_l'];
         $flickrPhoto->LargeWidth = $value['width_l'];
     }
     $flickrPhoto->OriginalURL = $value['url_o'];
     $flickrPhoto->OriginalHeight = $value['height_o'];
     $flickrPhoto->OriginalWidth = $value['width_o'];
     $flickrPhoto->Description = 'test';
     // $value['description']['_content'];
     $author = FlickrAuthor::get()->filter('PathAlias', $pathalias)->first();
     if (!$author) {
         $author = new FlickrAuthor();
         $author->PathAlias = $pathalias;
         $author->write();
     }
     $flickrPhoto->PhotographerID = $author->ID;
     $lat = number_format($value['latitude'], 15);
     $lon = number_format($value['longitude'], 15);
     if ($value['latitude']) {
         $flickrPhoto->Lat = $lat;
         $flickrPhoto->ZoomLevel = 15;
     }
     if ($value['longitude']) {
         $flickrPhoto->Lon = $lon;
     }
     if ($value['accuracy']) {
         $flickrPhoto->Accuracy = $value['accuracy'];
     }
     if (isset($value['geo_is_public'])) {
         $flickrPhoto->GeoIsPublic = $value['geo_is_public'];
     }
     if (isset($value['woeid'])) {
         $flickrPhoto->WoeID = $value['woeid'];
     }
     $singlePhotoInfo = $this->f->photos_getInfo($flickrPhotoID);
     $flickrPhoto->Description = $singlePhotoInfo['photo']['description']['_content'];
     $flickrPhoto->TakenAt = $singlePhotoInfo['photo']['dates']['taken'];
     $flickrPhoto->Rotation = $singlePhotoInfo['photo']['rotation'];
     if (isset($singlePhotoInfo['photo']['visibility'])) {
         $flickrPhoto->IsPublic = $singlePhotoInfo['photo']['visibility']['ispublic'];
     }
     $flickrPhoto->write();
     foreach ($singlePhotoInfo['photo']['tags']['tag'] as $key => $taginfo) {
         $tag = DataObject::get_one('FlickrTag', "\"Value\"='" . $taginfo['_content'] . "'");
         if (!$tag) {
             $tag = new FlickrTag();
         }
         $tag->FlickrID = $taginfo['id'];
         $tag->Value = $taginfo['_content'];
         $tag->RawValue = $taginfo['raw'];
         $tag->write();
         $ftags = $flickrPhoto->FlickrTags();
         $ftags->add($tag);
         $flickrPhoto->write();
         $tag = NULL;
         $ftags = NULL;
         gc_collect_cycles();
     }
     return $flickrPhoto;
 }