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

Commit

Permalink
Contents' years editing added, fixes #350.
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Jun 21, 2013
1 parent d8283af commit fb0ba9c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
50 changes: 50 additions & 0 deletions controllers/jsapi.php
Expand Up @@ -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());
}
4 changes: 3 additions & 1 deletion helpers/tpl.php
Expand Up @@ -199,4 +199,6 @@ function tpl_file($file) {
);
}

?>
function tpl_year($y) {
return $y . '/' . ($y + 1);
}
4 changes: 3 additions & 1 deletion routes.php
Expand Up @@ -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
Expand Down
55 changes: 54 additions & 1 deletion views/static/scripts/usercontents.js
Expand Up @@ -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();
});

});
11 changes: 11 additions & 0 deletions views/static/styles/global.css
Expand Up @@ -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 {

Expand Down

0 comments on commit fb0ba9c

Please sign in to comment.