public function execute()
 {
     $this->photo_ids = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $album_id = waRequest::post('album_id', array(), waRequest::TYPE_ARRAY_INT);
     $copy = waRequest::post('copy', 1, waRequest::TYPE_INT);
     $this->album_photos_model = new photosAlbumPhotosModel();
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$copy) {
         // it means manage with one photo
         $photo_id = $this->photo_ids[0];
         if (!$photo_rights_model->checkRights($photo_id, true)) {
             throw new waException("You don't have sufficient access rights");
         }
         $early_albums = array_keys($this->album_photos_model->getByField('photo_id', $photo_id, 'album_id'));
         // TODO: check rights for editing (take into account deleting!)
         $this->album_photos_model->set($photo_id, $album_id);
         $this->log('photos_move', 1);
         $albums = $this->getAlbumsCounters();
         $old_albums = array();
         foreach ($early_albums as $a_id) {
             if (!isset($albums[$a_id])) {
                 $collection = new photosCollection('/album/' . $a_id);
                 $album = array('id' => $a_id, 'count' => $collection->count(), 'count_new' => 0);
                 $old_albums[] = $album;
             }
         }
         $this->response['albums'] = array_values($albums);
         $this->response['old_albums'] = $old_albums;
     } else {
         // otherwise coping photos to albums
         $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($this->photo_ids, true);
         $denied_photo_id = array_values(array_diff($this->photo_ids, $allowed_photo_id));
         $album_rights_model = new photosAlbumRightsModel();
         $allowed_album_id = $album_rights_model->filterAllowedAlbumIds($album_id, true);
         $denied_album_id = array_values(array_diff($album_id, $allowed_album_id));
         if ($allowed_album_id && $allowed_photo_id) {
             $this->album_photos_model->add($allowed_photo_id, $allowed_album_id);
             $this->response['albums'] = array_values($this->getAlbumsCounters());
             $this->log('photos_move', 1);
         }
         if ($denied_photo_id) {
             $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", count($denied_photo_id), _w("out of %d selected", "out of %d selected", count($this->photo_ids))) . ', ' . _w("because you don't have sufficient access rights") . '.';
         }
     }
 }
 public function execute()
 {
     $album_id = waRequest::post('album_id', null, waRequest::TYPE_INT);
     if ($album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         $this->response['photo_id'] = array_keys($album_photos_model->getByField('album_id', $album_id, 'photo_id'));
     } else {
         $photo_model = new photosPhotoModel();
         $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
         $this->response['photo_id'] = array_keys($photo_model->getPhotos($photo_id));
     }
 }
 public function execute()
 {
     $this->photo_ids = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $album_id = waRequest::post('album_id', array(), waRequest::TYPE_ARRAY_INT);
     $copy = waRequest::post('copy', 1, waRequest::TYPE_INT);
     $this->album_photos_model = new photosAlbumPhotosModel();
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$copy) {
         // it means manage with one photo
         $photo_id = $this->photo_ids[0];
         if (!$photo_rights_model->checkRights($photo_id, true)) {
             throw new waException("You don't have sufficient access rights");
         }
         $early_albums = array_keys($this->album_photos_model->getByField('photo_id', $photo_id, 'album_id'));
         // TODO: check rights for editing (take into account deleting!)
         $this->album_photos_model->set($photo_id, $album_id);
         $this->log('photos_move', 1);
         $albums = $this->getAlbumsCounters();
         $old_albums = array();
         foreach ($early_albums as $a_id) {
             if (!isset($albums[$a_id])) {
                 $collection = new photosCollection('/album/' . $a_id);
                 $album = array('id' => $a_id, 'count' => $collection->count(), 'count_new' => 0);
                 $old_albums[] = $album;
             }
         }
         $this->response['albums'] = array_values($albums);
         $this->response['old_albums'] = $old_albums;
     } else {
         // otherwise copy photos to albums
         $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($this->photo_ids, true);
         $denied_photo_id = array_values(array_diff($this->photo_ids, $allowed_photo_id));
         $album_rights_model = new photosAlbumRightsModel();
         $allowed_album_id = $album_rights_model->filterAllowedAlbumIds($album_id, true);
         $denied_album_id = array_values(array_diff($album_id, $allowed_album_id));
         if ($allowed_album_id && $allowed_photo_id) {
             $this->album_photos_model->add($allowed_photo_id, $allowed_album_id);
             $this->log('photos_move', 1);
         }
         $albums = $this->getAlbumsCounters();
         $this->response['albums'] = array_values($albums);
         if ($denied_photo_id) {
             $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", count($denied_photo_id), _w("out of %d selected", "out of %d selected", count($this->photo_ids))) . ', ' . _w("because you don't have sufficient access rights") . '.';
         }
     }
     // Set cover photos for albums if first photo just been added to it
     $photo_model = new photosPhotoModel();
     $album_model = new photosAlbumModel();
     $allowed_photo_id = isset($allowed_photo_id) ? $allowed_photo_id : array($photo_id);
     $no_cover_album_ids = array();
     foreach (ifset($allowed_album_id, $album_id) as $album_id) {
         if (!empty($albums[$album_id]) && empty($albums[$album_id]['key_photo_id'])) {
             $no_cover_album_ids[] = $album_id;
         }
     }
     $photos = array();
     while ($allowed_photo_id && $no_cover_album_ids) {
         // Get random photo from added and make sure it exists
         shuffle($allowed_photo_id);
         $photo_id = array_pop($allowed_photo_id);
         if (!empty($photos[$photo_id])) {
             $photo = $photos[$photo_id];
         } else {
             $photos[$photo_id] = $photo = $photo_model->getById($photo_id);
         }
         if (!$photo) {
             continue;
         }
         // Photo exists, so add it back
         $allowed_photo_id[] = $photo_id;
         // Set cover for one album
         $album_id = array_pop($no_cover_album_ids);
         $album_model->updateById($album_id, array('key_photo_id' => $photo_id));
         photosPhoto::generateThumbs($photo, array('192x192'));
     }
 }
 protected function save(waRequestFile $file)
 {
     // check image
     if (!($image = $file->waImage())) {
         throw new waException(_w('Incorrect image'));
     }
     $exif_data = photosExif::getInfo($file->tmp_name);
     $image_changed = false;
     if (!empty($exif_data['Orientation'])) {
         $image_changed = $this->correctOrientation($exif_data['Orientation'], $image);
     }
     /**
      * Extend upload proccess
      * Make extra workup
      * @event photo_upload
      */
     $event = wa()->event('photo_upload', $image);
     if ($event && !$image_changed) {
         foreach ($event as $plugin_id => $result) {
             if ($result) {
                 $image_changed = true;
                 break;
             }
         }
     }
     $data = array('name' => preg_replace('/\\.[^\\.]+$/', '', basename($file->name)), 'ext' => $file->extension, 'size' => $file->size, 'type' => $image->type, 'width' => $image->width, 'height' => $image->height, 'contact_id' => $this->getUser()->getId(), 'status' => $this->status, 'upload_datetime' => date('Y-m-d H:i:s'));
     if ($this->status <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     }
     $photo_id = $data['id'] = $this->model->insert($data);
     if (!$photo_id) {
         throw new waException(_w('Database error'));
     }
     // update url
     $url = $this->generateUrl($data['name'], $photo_id);
     $this->model->updateById($photo_id, array('url' => $url));
     // check rigths to upload folder
     $photo_path = photosPhoto::getPhotoPath($data);
     if (file_exists($photo_path) && !is_writable($photo_path) || !file_exists($photo_path) && !waFiles::create($photo_path)) {
         $this->model->deleteById($photo_id);
         throw new waException(sprintf(_w("The insufficient file write permissions for the %s folder."), substr($photo_path, strlen($this->getConfig()->getRootPath()))));
     }
     if ($image_changed) {
         $image->save($photo_path);
         // save original
         if ($this->getConfig()->getOption('save_original')) {
             $original_file = photosPhoto::getOriginalPhotoPath($photo_path);
             $file->moveTo($original_file);
         }
     } else {
         $file->moveTo($photo_path);
     }
     unset($image);
     // free variable
     // add to album
     if ($photo_id && $this->album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         // update note if album is empty and note is yet null
         $r = $album_photos_model->getByField('album_id', $this->album_id);
         if (!$r) {
             $album_model = new photosAlbumModel();
             $sql = "UPDATE " . $album_model->getTableName() . " SET note = IFNULL(note, s:note) WHERE id = i:album_id";
             $time = !empty($exif_data['DateTimeOriginal']) ? strtotime($exif_data['DateTimeOriginal']) : time();
             $album_model->query($sql, array('note' => mb_strtolower(_ws(date('F', $time))) . ' ' . _ws(date('Y', $time)), 'album_id' => $this->album_id));
         }
         // add to album iteself
         $sort = (int) $album_photos_model->query("SELECT sort + 1 AS sort FROM " . $album_photos_model->getTableName() . " WHERE album_id = i:album_id ORDER BY sort DESC LIMIT 1", array('album_id' => $this->album_id))->fetchField('sort');
         $album_photos_model->insert(array('photo_id' => $photo_id, 'album_id' => $this->album_id, 'sort' => $sort));
     }
     // save rights for groups
     if ($this->groups) {
         $rights_model = new photosPhotoRightsModel();
         $rights_model->multiInsert(array('photo_id' => $photo_id, 'group_id' => $this->groups));
     }
     // save exif data
     if (!empty($exif_data)) {
         $exif_model = new photosPhotoExifModel();
         $exif_model->save($photo_id, $exif_data);
     }
     $sizes = $this->getConfig()->getSizes();
     photosPhoto::generateThumbs($data, $sizes);
     return array('name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'thumbnail_url' => photosPhoto::getPhotoUrl($data, photosPhoto::getThumbPhotoSize()), 'url' => '#/photo/' . $photo_id . '/');
 }
 private function getPhotoOffsetCondition($photo, $order, $eq = false)
 {
     $order = trim($order);
     $order = explode(' ', $order);
     if (!isset($order[1])) {
         $order[1] = 'asc';
     }
     $order[1] = strtolower($order[1]);
     list($t, $f) = explode('.', $order[0]);
     if ($t == 'ap' && $this->update_count) {
         $model = new photosAlbumPhotosModel();
         $row = $model->getByField(array('album_id' => $this->update_count['id'], 'photo_id' => $photo['id']));
         $v = $row['sort'];
     } else {
         $v = $photo[$f];
     }
     // return condition
     return $order[0] . ($eq ? ' = ' : ($order[1] == 'asc' ? ' < ' : ' > ')) . "'{$v}'";
 }
 private function _makeStack($parent_id, $photo_ids, $op = 'make')
 {
     $photo_ids = (array) $photo_ids;
     if ($op == 'make') {
         $where = $this->getWhereByField('id', $photo_ids);
         // get description - first not empty description but description of parent is first-priority
         $sql = "SELECT description FROM {$this->table} WHERE id = i:parent_id AND description IS NOT NULL\n                    UNION\n                    SELECT description FROM {$this->table} WHERE {$where} AND description IS NOT NULL LIMIT 0,1";
         $description = $this->query($sql, array('parent_id' => $parent_id))->fetchField('description');
         // get max rate of all photos
         $sql = "SELECT MAX(rate) rate FROM {$this->table} WHERE id = i:parent_id OR {$where}";
         $rate = $this->query($sql, array('parent_id' => $parent_id))->fetchField('rate');
         // get status
         $sql = "SELECT status FROM {$this->table} WHERE id = i:parent_id";
         $status = $this->query($sql, array('parent_id' => $parent_id))->fetchField('status');
         $stack_count = 1;
         $sort = 1;
     } else {
         $parent = $this->getById($parent_id);
         $rate = $parent['rate'];
         $description = $parent['description'];
         $status = $parent['status'];
         $stack_count = $parent['stack_count'];
         // get last sort value of stack plus 1
         $sql = "SELECT sort FROM {$this->table} WHERE parent_id = i:parent_id ORDER BY sort DESC LIMIT 1";
         $sort = $this->query($sql, array('parent_id' => $parent_id))->fetchField('sort') + 1;
     }
     // get groups
     $photo_rights_model = new photosPhotoRightsModel();
     $groups = array_keys($photo_rights_model->getByField('photo_id', $parent_id, 'group_id'));
     // make first of all operations connected with file-manipulations
     foreach ($photo_ids as $id) {
         // update access
         $this->upAccess($id, array('status' => $status, 'groups' => $groups));
     }
     // make children of stack
     foreach ($photo_ids as $id) {
         $this->updateById($id, array('parent_id' => $parent_id, 'description' => $description, 'rate' => $rate, 'sort' => $sort++, 'stack_count' => 0));
     }
     // make parent of stack
     $this->updateById($parent_id, array('parent_id' => 0, 'description' => $description, 'rate' => $rate, 'stack_count' => $stack_count + count($photo_ids), 'sort' => 0));
     if ($op == 'make') {
         $photo_ids[] = $parent_id;
     } else {
         $photo_ids = array_keys($this->getByField('parent_id', $parent_id, 'id'));
         $photo_ids[] = $parent_id;
     }
     // merge tags for stack
     $photo_tags_model = new photosPhotoTagsModel();
     $tag_ids = array_keys($photo_tags_model->getByField('photo_id', $photo_ids, 'tag_id'));
     $photo_tags_model->assign($photo_ids, $tag_ids);
     // merge albums for stack
     $album_photos_model = new photosAlbumPhotosModel();
     $album_ids = array_keys($album_photos_model->getByField('photo_id', $photo_ids, 'album_id'));
     $album_photos_model->add($photo_ids, $album_ids, false);
 }