Exemplo n.º 1
0
 public function execute()
 {
     if (!wa()->getUser()->getRights('photos', 'upload')) {
         throw new waAPIException('access_denied', 403);
     }
     $data = waRequest::post();
     // check required param name
     $this->post('name', true);
     $album_model = new photosAlbumModel();
     $group_ids = array(0);
     if (!isset($data['status'])) {
         $data['status'] = 1;
     } else {
         if ($data['status'] == -1) {
             $group_ids = array(-wa()->getUser()->getId());
         }
     }
     if ($data['status'] <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     } else {
         $data['url'] = $album_model->suggestUniqueUrl(photosPhoto::suggestUrl($data['name']));
     }
     if (!isset($data['type'])) {
         $data['type'] == photosAlbumModel::TYPE_STATIC;
     }
     $parent_id = waRequest::post('parent_id', 0, 'int');
     $parent = $album_model->getById($parent_id);
     if ($parent_id) {
         if (!$parent) {
             throw new waAPIException('invalid_request', 'Parent album not found', 404);
         }
         if ($data['type'] == photosAlbumModel::TYPE_STATIC && $parent['type'] == photosAlbumModel::TYPE_DYNAMIC) {
             throw new waAPIException('invalid_request', 'Inserted album is static but parent album is dynamic', 404);
         }
         if ($data['status'] > 0 && $parent['status'] <= 0) {
             throw new waAPIException('invalid_request', 'Inserted album is public but parent album is private', 404);
         }
     }
     if ($id = $album_model->add($data, $parent_id)) {
         // return info of the new album
         $_GET['id'] = $id;
         if ($parent_id) {
             $child = $album_model->getFirstChild($parent_id);
             $album_model->move($id, $child ? $child['id'] : 0, $parent_id);
         }
         $album_rights_model = new photosAlbumRightsModel();
         $album_rights_model->setRights($id, $group_ids);
         $method = new photosAlbumGetInfoMethod();
         $this->response = $method->getResponse(true);
     } else {
         throw new waAPIException('server_error', 500);
     }
 }
 public function execute()
 {
     $this->id = waRequest::post('id', null, waRequest::TYPE_INT);
     $this->album_model = new photosAlbumModel();
     $parent = null;
     $parent_id = waRequest::get('parent_id', 0, waRequest::TYPE_INT);
     if ($parent_id) {
         $parent = $this->album_model->getById($parent_id);
     }
     $url = waRequest::post('url', null, waRequest::TYPE_STRING_TRIM);
     $group_ids = null;
     $status = waRequest::post('status', 0, waRequest::TYPE_INT);
     if (!$status) {
         $group_ids = waRequest::post('groups', array(), waRequest::TYPE_ARRAY_INT);
         if (!$group_ids) {
             // visible only for creator
             $status = -1;
             $group_ids = array(-$this->getUser()->getId());
         }
     }
     $name = waRequest::post('name', '', waRequest::TYPE_STRING_TRIM);
     if ($name === '0') {
         $name = '0 ';
     }
     if (!$this->id) {
         if (!$this->getRights('upload')) {
             throw new waException(_w("You don't have sufficient access rights"));
         }
         if ($parent && $parent['status'] <= 0 && $status == 1) {
             throw new waException(_w("Parent album is private"));
         }
         $type = waRequest::post('type', 0, waRequest::TYPE_INT);
         if ($parent && $parent['type'] == photosAlbumModel::TYPE_DYNAMIC && $type == photosAlbumModel::TYPE_STATIC) {
             throw new waException(_w("Parent album is smart"));
         }
         $data = array('name' => $name, 'status' => $status, 'type' => $type, 'group_ids' => $group_ids);
         if ($status <= 0) {
             $data['hash'] = md5(uniqid(time(), true));
         } else {
             $data['url'] = $this->album_model->suggestUniqueUrl(photosPhoto::suggestUrl(strlen($url) ? $url : $name));
         }
         if ($type == photosAlbumModel::TYPE_DYNAMIC) {
             $data['conditions'] = $this->getPrepareConditions();
         }
         $this->save($data);
         if ($parent) {
             $child = $this->album_model->getFirstChild($parent['id']);
             $this->album_model->move($this->id, $child ? $child['id'] : 0, $parent['id']);
         }
         $this->response = array('id' => $this->id, 'name' => photosPhoto::escape($name), 'type' => $type, 'status' => $status);
     } else {
         $album_rights_model = new photosAlbumRightsModel();
         if (!$album_rights_model->checkRights($this->id, true)) {
             throw new waException(_w("You don't have sufficient access rights"));
         }
         $conditions = $this->getPrepareConditions();
         $params = array();
         $album_params = waRequest::post('params', '', waRequest::TYPE_STRING_TRIM);
         $album_params = explode(PHP_EOL, $album_params);
         foreach ($album_params as $param) {
             $param = explode('=', $param);
             if (count($param) < 2) {
                 continue;
             }
             $params[$param[0]] = trim($param[1]);
         }
         $params = $params ? $params : null;
         $description = waRequest::post('description', null, waRequest::TYPE_STRING_TRIM);
         $data = array('status' => $status, 'group_ids' => $group_ids, 'conditions' => $conditions, 'url' => $url, 'description' => $description, 'params' => $params, 'name' => $name);
         if ($status <= 0) {
             $data['hash'] = md5(uniqid(time(), true));
         }
         if (waRequest::post('order') == 'rate') {
             $data['params']['order'] = 'rate';
         }
         if (!$this->validate($data)) {
             return;
         }
         $this->save($data);
         $apply_all_photos = waRequest::post('apply_all_photos', 0, waRequest::TYPE_INT);
         if ($apply_all_photos) {
             // apply to first of $count photos
             $count = waRequest::post('count', 50, waRequest::TYPE_INT);
             $collection = new photosCollection('album/' . $this->id);
             $total_count = $collection->count();
             $photos = $collection->getPhotos('*', 0, $count, false);
             $photo_model = new photosPhotoModel();
             $photo_ids = array();
             foreach ($photos as $photo) {
                 if ($photo['status'] == 1 && $status == 1) {
                     continue;
                 }
                 if ($photo['stack_count'] > 0) {
                     $photo_ids = array_merge($photo_ids, $photo_model->getIdsByParent($photo['id']));
                 } else {
                     $photo_ids[] = $photo['id'];
                 }
             }
             $photo_rights_model = new photosPhotoRightsModel();
             $allowed_photo_ids = $photo_rights_model->filterAllowedPhotoIds($photo_ids, true);
             $photo_model->updateAccess($allowed_photo_ids, $status, $group_ids);
             $this->response['total_count'] = $total_count;
             $this->response['count'] = $count;
             $this->response['status'] = $status;
             $this->response['groups'] = $group_ids;
         }
     }
 }