Skip to content

Instantly share code, notes, and snippets.

@Largo
Last active August 29, 2015 14:22
Show Gist options
  • Save Largo/d7371d4ba8c8846fdc38 to your computer and use it in GitHub Desktop.
Save Largo/d7371d4ba8c8846fdc38 to your computer and use it in GitHub Desktop.
No warranties. This is very beta extension for implementing the drag and drop order function in bolt. you need a taxonomy "sortierung" and shouldn't have any other taxonomies, because it might not work
<?php
namespace Bolt\Extension\Largo\SortOrder;
use Symfony\Component\HttpFoundation\Response;
use Bolt;
class Extension extends \Bolt\BaseExtension
{
/** @var boolean */
private $isAdmin;
public function getName() {
return "SortOrder";
}
function info() {
$data = array(
'name' =>"Sort Order",
'description' => "This extension reacts to the event when you reorder something in a content type.",
'author' => "Largo",
'link' => "http://bolt.cm",
'version' => "1.0",
'required_bolt_version' => "2.2",
'highest_bolt_version' => "2.2",
'type' => "",
'first_releasedate' => "2015-06-04",
'latest_releasedate' => "2015-06-04",
);
return $data;
}
// This checks if a user is a dashboard user
public function isAdmin()
{
if (is_null($this->isAdmin)) {
// check if user has allowed role(s)
$user = $this->app['users']->getCurrentUser();
$userid = $user['id'];
$this->isAdmin = false;
//foreach ($this->config['admin_roles'] as $role) {
// if ($this->app['users']->hasRole($userid, 'chief-editor')) {
if ($user) { //&& $user->isAllowed('login')
$this->isAdmin = true;
// break;
}
// }
}
return $this->isAdmin;
}
function initialize() {
$this->app->match("/bolt/overview/undefinedcontent/sortcontent/{contenttype}", array($this, 'saveorder'));
}
public function saveorder($contenttype) {
if(!$this->isAdmin()) {
echo "bye"; exit;
}
$items = $this->app['request']->get('item');
$body = "success";
$content = $this->app['storage']->getContent(
$contenttype,
array('limit' => 10000, 'order' => 'datepublish desc', 'hydrate' => true)
);
foreach ($content as $entry) {
$entry->sortorder = (int) array_search($entry->id, $items);
$entry->group['order'] = $entry->sortorder;
$entry->setTaxonomy('sortierung', 'nicht_sortiert', 'nicht_sortiert', 0);
$this->app['storage']->saveContent($entry);
$entry->setTaxonomy('sortierung', 'sortierung', 'Beiträge', (int) array_search($entry->id, $items));
$this->app['storage']->saveContent($entry);
}
foreach ($content as $entry) {
$this->app['db']->executeQuery("UPDATE bolt_taxonomy SET sortorder = '". (int) array_search($entry->id, $items) ."' WHERE bolt_taxonomy.content_id = ". $entry->id .";");
}
$headers = array();
return new Response($body, 200, $headers);
}
}
@Largo
Copy link
Author

Largo commented Jun 4, 2015

My Taxonomy:

sortierung:
slug: sortierung
singular_slug: sortierung
behaves_like: grouping
prefix: "Hier wird die Sortierung gespeichert"
options: { sortierung: 'sortierung', nicht_sortiert : 'nicht sortiert' }
has_sortorder: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment