Skip to content

Commit

Permalink
change order of sorted articles
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Feb 24, 2015
1 parent f873971 commit b015ad6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
11 changes: 5 additions & 6 deletions newscoop/library/Newscoop/Services/PlaylistsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @copyright 2015 Sourcefabric o.p.s.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\Services;

use Newscoop\Entity\Article;
Expand Down Expand Up @@ -158,8 +157,8 @@ private function positionPlaylistArticle($playlist, $playlistArticle, $position)
->setParameter('playlistId', $playlist->getId())
->getSingleScalarResult();

if ($position > ((int)$maxPosition) ) {
$position = (int)$maxPosition;
if ($position > ((int) $maxPosition)) {
$position = (int) $maxPosition;
}

// get article - move to position 0
Expand Down Expand Up @@ -187,10 +186,10 @@ private function positionPlaylistArticle($playlist, $playlistArticle, $position)
$playlistArticle->setOrder($position);
$this->em->flush();

if ($oldOrder == 0 && $playlist->getMaxPosition() !== null && (int)$maxPosition+1 >= $playlist->getMaxPosition()) {
if ($oldOrder == 0 && $playlist->getMaxItems() !== null && (int) $maxPosition+1 >= $playlist->getMaxItems()) {
$this->em
->createQuery('DELETE FROM Newscoop\Entity\PlaylistArticle pa WHERE pa.order > :maxPosition AND pa.idPlaylist = :playlistId')
->setParameter('maxPosition', $playlist->getMaxPosition())
->setParameter('maxPosition', $playlist->getMaxItems())
->setParameter('playlistId', $playlist->getId())
->execute();
}
Expand All @@ -200,4 +199,4 @@ private function positionPlaylistArticle($playlist, $playlistArticle, $position)
$this->em->getConnection()->exec('UNLOCK TABLES;');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @copyright 2012 Sourcefabric o.p.s.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\GimmeBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function getArticlesListsAction(Request $request)

$paginator = $this->get('newscoop.paginator.paginator_service');
$playlists = $paginator->paginate($playlists, array(
'distinct' => false
'distinct' => false,
));

return $playlists;
Expand Down Expand Up @@ -118,7 +117,7 @@ public function getPlaylistsArticlesAction(Request $request, $id)

$playlist = $em->getRepository('Newscoop\Entity\Playlist')
->findOneBy(array(
'id' => $id
'id' => $id,
));

if (!$playlist) {
Expand All @@ -131,7 +130,8 @@ public function getPlaylistsArticlesAction(Request $request, $id)
if ($user && $user->isAdmin()) {
$onlyPublished = false;
}
} catch (\Newscoop\NewscoopException $e) {}
} catch (\Newscoop\NewscoopException $e) {
}

$playlistArticles = $em->getRepository('Newscoop\Entity\Playlist')
->articles($playlist, null, true, null, null, $onlyPublished, true)->getResult();
Expand All @@ -143,14 +143,14 @@ public function getPlaylistsArticlesAction(Request $request, $id)

$paginator = $this->get('newscoop.paginator.paginator_service');
$articles = $paginator->paginate($articles, array(
'distinct' => false
'distinct' => false,
));

$allItems = array_merge(array(
'id' => $playlist->getId(),
'title' => $playlist->getName(),
'notes' => $playlist->getNotes(),
'maxItems' => $playlist->getMaxItems()
'maxItems' => $playlist->getMaxItems(),
), $articles);

return $allItems;
Expand Down Expand Up @@ -196,7 +196,7 @@ public function linkToPlaylistAction(Request $request, $id)
if (!$playlist) {
throw new NotFoundHttpException('Result was not found.');
}

$this->linkOrUnlinkResources($playlist, $request, 'link');

return $playlist;
Expand Down Expand Up @@ -290,7 +290,7 @@ public function saveBatchActionsAction(Request $request, $id)
if (!$playlist) {
throw new NotFoundHttpException('Result was not found.');
}

$actions = $request->request->get('actions', array());

// The controller resolver needs a request to resolve the controller.
Expand Down Expand Up @@ -334,20 +334,20 @@ public function saveBatchActionsAction(Request $request, $id)

try {
$result = call_user_func_array($controller, $arguments);

if (!is_object($result)) {
continue;
}
$actionsResults[$actionKey] = array(
'object' => $result,
'method' => $method,
'header' => $header
'header' => $header,
);
} catch (\Exception $e) {
$actionsResults[$actionKey] = array(
'object' => $e,
'method' => $method,
'header' => $header
'header' => $header,
);

continue;
Expand Down Expand Up @@ -384,7 +384,7 @@ private function linkOrUnlinkResources($playlist, $request, $action = 'link')
}
$playlistService = $this->get('playlists');
$playlistService->addArticle($playlist, $object, $position);
} else if ($action == 'unlink') {
} elseif ($action == 'unlink') {
$playlistService = $this->get('playlists');
$playlistService->removePlaylistArticle($playlist, $object);
}
Expand Down Expand Up @@ -416,7 +416,7 @@ private function getNotConvertedLinks($request)

$links[] = array(
'resource' => $resource,
'resourceType' => $resourceType
'resourceType' => $resourceType,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,23 @@ app.controller('FeaturedController', [

$scope.sortableConfig = {
group: 'articles',
animation: 150
animation: 150,
onSort: function (evt/**Event*/){
var article = evt.model;
var isInLogList = false;
isInLogList = _.some(
Playlist.getLogList(),
{number: article.number}
);

// only when sorting list of featured articles (playlist)
var logList = Playlist.getLogList();
article._order = evt.newIndex + 1;
article._method = "link";
if (!isInLogList) {
Playlist.addItemToLogList(article);
}
}
};


Expand Down Expand Up @@ -135,8 +151,8 @@ app.controller('FeaturedController', [
if (!isInLogList) {
// set method for the removed object so we can pass it to
// the API endpoint and remove item using batch remove feature
item._method = "unlink";
Playlist.addItemToLogList(item);
article._method = "unlink";
Playlist.addItemToLogList(article);
} else {
Playlist.removeItemFromLogList(article.number, 'link');
}
Expand Down Expand Up @@ -184,7 +200,7 @@ app.controller('PlaylistsController', [
}
});

if (occurences != 1) {
if (occurences > 1) {
$scope.featuredArticles.splice(evt.newIndex, 1);
flashMessage(Translator.trans('Item already exists in the list'), 'error');

Expand All @@ -199,10 +215,13 @@ app.controller('PlaylistsController', [
if (!isInLogList) {
// add article to log list, so we can save it later using batch save
item._method = "link";
item._order = evt.newIndex + 1;
Playlist.addItemToLogList(item);
} else {
Playlist.removeItemFromLogList(number, 'unlink');
}

console.log(Playlist.getLogList());
}
};

Expand Down Expand Up @@ -298,7 +317,7 @@ app.controller('PlaylistsController', [
Playlist.batchUpdate(logList)
.then(function () {
flashMessage(Translator.trans('List saved'));
logList = [];
Playlist.clearLogList();
}, function() {
flashMessage(Translator.trans('Could not save the list'), 'error');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ angular.module('playlistsApp').factory('Playlist', [
{number: article.number, language: article.language},
false
),
'; rel="article">'].join('');
'; rel="article">'].join('');;

if (article._method == 'link') {
var position = [',<',
article._order,
'; rel="article-position">'].join('');
link = link.concat(position);
}

postParams.push({link: link, method: article._method});
});

Expand Down Expand Up @@ -226,6 +234,17 @@ angular.module('playlistsApp').factory('Playlist', [
return logList;
};

/**
* Clears log list
*
* @return {Array} Log list
*/
Playlist.clearLogList = function () {
logList = [];

return logList;
};

/**
* Adds new item to the logList
* @param {Object} article Article object
Expand Down

0 comments on commit b015ad6

Please sign in to comment.