From fb0ba9c5616a7ca71238cd30df11179fe6b5cac3 Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Fri, 21 Jun 2013 23:06:09 +0200 Subject: [PATCH] Contents' years editing added, fixes #350. --- controllers/jsapi.php | 50 +++++++++++++++++++++++++ helpers/tpl.php | 4 +- routes.php | 4 +- views/static/scripts/usercontents.js | 55 +++++++++++++++++++++++++++- views/static/styles/global.css | 11 ++++++ 5 files changed, 121 insertions(+), 3 deletions(-) diff --git a/controllers/jsapi.php b/controllers/jsapi.php index 54f8e42..0316d41 100644 --- a/controllers/jsapi.php +++ b/controllers/jsapi.php @@ -153,3 +153,53 @@ function jsapi_post_change_content_title() { return htmlentities($content->getTitle(), ENT_COMPAT, 'UTF-8'); } + +// used by Jeditable +function jsapi_post_change_content_year() { + + $content_id = (int)get_string('id', 'POST'); + $newvalue = get_string('value', 'POST'); + + $content = ContentQuery::create() + ->filterByDeleted(false) + ->findOneById($content_id); + + if (!$content) { halt(HTTP_NOT_FOUND); } + + $len = strlen($newvalue); + + if ($len === 0) { + return tpl_year($content->getYear()); + } + + $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); + + } + + $title_is_taken = ContentQuery::create() + ->filterByCursus($cursus) + ->filterByCourseId($content->getCourseId()) + ->filterByTitle($content->getTitle()) + ->filterByDeleted(0) + ->findOneByYear($newvalue); + + if ($title_is_taken && $title_is_taken->getId() !== $content->getId()) { + + halt(HTTP_BAD_REQUEST); + } + + $content->setYear($newvalue); + $content->save(); + + return tpl_year($content->getYear()); +} diff --git a/helpers/tpl.php b/helpers/tpl.php index d691bfd..d92fea5 100644 --- a/helpers/tpl.php +++ b/helpers/tpl.php @@ -199,4 +199,6 @@ function tpl_file($file) { ); } -?> +function tpl_year($y) { + return $y . '/' . ($y + 1); +} diff --git a/routes.php b/routes.php index 1dd295d..f240a7d 100644 --- a/routes.php +++ b/routes.php @@ -191,8 +191,10 @@ function redirect_profile_init() { redirect_to('/profil/créer'); } 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 + # Edit content's titles dispatch_post('/jsapi/edit/content/title.html', 'jsapi_post_change_content_title'); + # Edit content's years + dispatch_post('/jsapi/edit/content/year.html', 'jsapi_post_change_content_year'); } # pages diff --git a/views/static/scripts/usercontents.js b/views/static/scripts/usercontents.js index 1fafddf..46f5aae 100644 --- a/views/static/scripts/usercontents.js +++ b/views/static/scripts/usercontents.js @@ -4,9 +4,62 @@ $(function() { prettyPrint(); } + var cid = $( '.user_content' ).first().data( 'contentId' ), + years = {}, + d = new Date(), + // if we are after august 2042 (for example), the current year is 2042/2043, + // while if we are before, it's 2041/2042. + year = d.getMonth > 9 ? d.getFullYear() : d.getFullYear() - 1, + $cyear = $('.content_header .content-year').first(), + cyear = $cyear.text(), + i=2006, text; + + for (; i<=year; i++) { + text = i + '/' + (i + 1); + years[i] = text; + + if (!years.selected && text === cyear) { + years.selected = i; + } + } + IP7W.setEditable({ target: $( '.content_header h1' ).first(), - id: $( '.user_content' ).first().data( 'contentId' ), + id: cid, saveurl: '/jsapi/edit/content/title.html' }); + + IP7W.setEditable({ + target: $cyear, + id: cid, + type: 'select', + event: 'click', + data: years, + indicator: cyear, + saveurl: '/jsapi/edit/content/year.html', + onblur: 'cancel'/*, + + // FIXME: this is not error-proof, since that if the year + // cannot be changed and the server return an error, we won't know about + // it, and won't return to the previous title/path. Unfortunately Jeditable + // does not provide any 'success/error' callbacks, so we may have to use + // a custom function to perform the AJAX stuff. + callback: function( cyear2 ) { + if ( !window.history + || typeof window.history.replaceState !== 'function') { + return; + } + + history.replaceState(null, '', + window.location.pathname.replace(cyear.replace('/', '-'), + cyear2.replace('/', '-'))); + document.title = document.title.replace(cyear, cyear2); + cyear = cyear2; + }*/ + }); + + $cyear.on('change', 'select', function() { + $cyear.find( 'form' ).first().submit(); + }); + }); diff --git a/views/static/styles/global.css b/views/static/styles/global.css index bd1e8e8..fbb0256 100644 --- a/views/static/styles/global.css +++ b/views/static/styles/global.css @@ -595,6 +595,17 @@ a.news-title { .content_header a.user { color: #222; } +.content_header .content-year form { + display: inline; + margin:0 -2px; + cursor: pointer; +} +.content_header .content-year select { + background: transparent; + border: 0; + margin:0 -1px; + line-height: 1; +} .content-footer {