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()
 {
     $id = $this->get('id', true);
     $album_model = new photosAlbumModel();
     $album = $album_model->getById($id);
     if ($album) {
         $album_rights_model = new photosAlbumRightsModel();
         if (!$album_rights_model->checkRights($id, true)) {
             throw new waAPIException('access_denied', 403);
         }
         $data = waRequest::post();
         if (isset($data['parent_id']) && $album['parent_id'] != $data['parent_id']) {
             if (!$album_model->getById($data['parent_id'])) {
                 throw new waAPIException('invalid_param', 'Parent album not found', 404);
             }
             if (!$album_model->move($id, null, $data['parent_id'])) {
                 throw new waAPIException('server_error', 500);
             }
         }
         if (isset($data['type'])) {
             unset($data['type']);
         }
         if ($album_model->update($id, $data)) {
             // correct rights
             $album = $album_model->getById($id);
             $group_ids = array(0);
             if ($data['status'] == -1) {
                 $group_ids = array(-wa()->getUser()->getId());
             }
             $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);
         }
     } else {
         throw new waAPIException('invalid_param', 'Album not found', 404);
     }
 }
 private function save($data)
 {
     if (!$this->id) {
         $this->log('album_create', 1);
         $this->id = $this->album_model->add($data);
     } else {
         $album = $this->album_model->getById($this->id);
         if (!$album) {
             throw new Exception("Album doesn't exist");
         }
         $name = $album['name'];
         if (empty($data['name'])) {
             $data['name'] = $name;
         }
         if ($album['type'] != photosAlbumModel::TYPE_DYNAMIC && isset($data['conditions'])) {
             unset($data['conditions']);
         }
         if ($data['status'] <= 0) {
             if (isset($data['url']) && !$data['url']) {
                 unset($data['url']);
             }
         } else {
             if (empty($data['url'])) {
                 $data['url'] = photosPhoto::suggestUrl($data['name']);
             }
         }
         $this->album_model->update($this->id, $data);
         $album_params = new photosAlbumParamsModel();
         $album_params->set($this->id, $data['params']);
     }
     $album_rights_model = new photosAlbumRightsModel();
     if ($data['status'] <= 0 && $data['group_ids']) {
         $album_rights_model->setRights($this->id, $data['group_ids']);
     } else {
         $album_rights_model->setRights($this->id, 0);
     }
 }