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); } }
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); } }
private function generateUrl($name, $photo_id) { $counter = 1; $original_url = photosPhoto::suggestUrl($name); $url = $original_url; while ($this->urlExists($url, $photo_id)) { $url = "{$original_url}_{$counter}"; $counter++; } return $url; }
/** * Update taking in account recursive nature of 'full_url' fields * * @param int $id * @param array $data */ public function update($id, $data) { $item = $this->getById($id); if (!$item) { return false; } if (isset($data['url'])) { $url = $data['url']; unset($data['url']); } if (!isset($data['status'])) { $data['status'] = $item['status']; } if ($data['status'] <= 0) { $data['full_url'] = null; if (!isset($data['hash'])) { $data['hash'] = md5(uniqid(time(), true)); } } else { unset($data['full_url']); } $this->updateById($id, $data); if ($data['status'] <= 0) { $this->privateDescendants($id); } elseif (isset($url)) { $this->updateUrl($id, $url); } else { $item = $this->getById($id); if (!$item['url']) { $url = suggestUniqueUrl(photosPhoto::suggestUrl($item['name'])); $this->updateUrl($id, $url); } else { if (!$item['full_url']) { $this->updateUrl($id, $item['url']); } } } return true; }