Skip to content
This repository has been archived by the owner on Feb 28, 2018. It is now read-only.

Commit

Permalink
contents' titles edition moved to /jsapi namespace (cf #345)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Jun 16, 2013
1 parent d37e22c commit 3f22340
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 57 deletions.
52 changes: 0 additions & 52 deletions controllers/api_contents.php
Expand Up @@ -45,55 +45,3 @@ function json_get_last_contents() {

return json(array('data' => $tpl_contents));
}

// used by Jeditable
function api_post_change_content_title() {

$content_id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');

$content = ContentQuery::create()->findOneById($content_id);

if (!$content) { halt(HTTP_NOT_FOUND); }

$len = strlen($newvalue);

if ($len === 0) {
return $content->getTitle();
}

$cursus = $content->getCursus();

// user can only edit the title if he/she is a moderator/admin and/or the
// content has not been validated yet and he/she is its author.
if ( !is_connected()
|| ( !is_moderator()
&& !user()->isResponsibleFor($cursus)
&& ($content->isValidated()
|| user()->getId() !== $content->getAuthorId() ))) {

halt(HTTP_FORBIDDEN);

}

if ($content->isDeleted()) {
halt(HTTP_NOT_FOUND);
}

$title_is_taken = ContentQuery::create()
->filterByCursus($cursus)
->filterByCourseId($content->getCourseId())
->filterByYear($content->getYear())
->filterByDeleted(0)
->findOneByTitle($newvalue);

if ($title_is_taken && $title_is_taken->getId() !== $content->getId()) {

halt(HTTP_BAD_REQUEST);
}

$content->setTitle($newvalue);
$content->save();

return htmlentities($content->getTitle(), ENT_COMPAT, 'UTF-8');
}
54 changes: 54 additions & 0 deletions controllers/jsapi.php
Expand Up @@ -99,3 +99,57 @@ function jsapi_post_cursus_intro() {
return tpl_render('utils/md.html', array('content'=>$cursus->getDescription()));

}

// === CONTENTS ===

// used by Jeditable
function jsapi_post_change_content_title() {

$content_id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');

$content = ContentQuery::create()->findOneById($content_id);

if (!$content) { halt(HTTP_NOT_FOUND); }

$len = strlen($newvalue);

if ($len === 0) {
return $content->getTitle();
}

$cursus = $content->getCursus();

// user can only edit the title if he/she is a moderator/admin and/or the
// content has not been validated yet and he/she is its author.
if ( !is_connected()
|| ( !is_moderator()
&& !user()->isResponsibleFor($cursus)
&& ($content->isValidated()
|| user()->getId() !== $content->getAuthorId() ))) {

halt(HTTP_FORBIDDEN);

}

if ($content->isDeleted()) {
halt(HTTP_NOT_FOUND);
}

$title_is_taken = ContentQuery::create()
->filterByCursus($cursus)
->filterByCourseId($content->getCourseId())
->filterByYear($content->getYear())
->filterByDeleted(0)
->findOneByTitle($newvalue);

if ($title_is_taken && $title_is_taken->getId() !== $content->getId()) {

halt(HTTP_BAD_REQUEST);
}

$content->setTitle($newvalue);
$content->save();

return htmlentities($content->getTitle(), ENT_COMPAT, 'UTF-8');
}
7 changes: 3 additions & 4 deletions routes.php
Expand Up @@ -153,10 +153,6 @@ function redirect_profile_init() { redirect_to('/profil/créer'); }
## API endpoints for Jeditable
if (strpos(LIM_REQUEST_URI, '/api/1/edit') === 0) {

### Contents
# Edit title
dispatch_post('/api/1/edit/content/title.html', 'api_post_change_content_title');

### Courses
# Edit intro text
dispatch('/api/1/edit/course/intro.md', 'api_get_course_intro_markdown');
Expand Down Expand Up @@ -194,6 +190,9 @@ function redirect_profile_init() { redirect_to('/profil/créer'); }
# Edit cursus' intros
dispatch('/jsapi/edit/cursus/intro.md', 'jsapi_get_cursus_intro_markdown');
dispatch_post('/jsapi/edit/cursus/intro.html', 'jsapi_post_cursus_intro');

# Edit content's title
dispatch_post('/jsapi/edit/content/title.html', 'jsapi_post_change_content_title');
}

# pages
Expand Down
2 changes: 1 addition & 1 deletion views/static/scripts/usercontents.js
Expand Up @@ -7,6 +7,6 @@ $(function() {
IP7W.setEditable({
target: $( '.content_header h1' ).first(),
id: $( '.user_content' ).first().data( 'contentId' ),
saveurl: '/api/1/edit/content/title.html'
saveurl: '/jsapi/edit/content/title.html'
});
});

0 comments on commit 3f22340

Please sign in to comment.