public function ajaxAction($table, $idField, $orderField)
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $ids = $request->getPost('ids');
            foreach($ids as $id) {
                $cleanIds[] = (int) $id;
            }
            $db = $this->getActionController()->db;
            $select = $db->select()->from($table, array($idField, $orderField))
                    ->where($idField . ' in(?)', $cleanIds)
                    ->order($orderField);

            $oldOrder = $db->fetchPairs($select);

            if(count($oldOrder) == count($cleanIds)) {
                $newOrder = array_combine($cleanIds, $oldOrder);

                $changed = 0;
                foreach($newOrder as $id => $order)
                {
                    $changed = $changed + $db->update($table,
                            array($orderField => $order),
                            $db->quoteInto($idField . ' = ?', $id)
                            );
                }

                $this->getActionController()->addMessage(sprintf($this->getActionController()->plural('%s record updated due to sorting.', '%s records updated due to sorting.', $changed), $changed));

                return;
            }
        }

        throw new \Gems_Exception($this->_('Sorting failed'), 403);
    }
public function ajaxAction($table, $idField, $orderField)
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $ids = $request->getPost('ids');
            foreach($ids as $id) {
                $cleanIds[] = (int) $id;
            }
            $db = $this->getActionController()->db;
            $select = $db->select()->from($table, array($idField, $orderField))
                    ->where($idField . ' in(?)', $cleanIds)
                    ->order($orderField);

            $oldOrder = $db->fetchPairs($select);

            if(count($oldOrder) == count($cleanIds)) {
                $newOrder = array_combine($cleanIds, $oldOrder);

                $changed = 0;
                foreach($newOrder as $id => $order)
                {
                    $changed = $changed + $db->update($table,
                            array($orderField => $order),
                            $db->quoteInto($idField . ' = ?', $id)
                            );
                }

                $this->getActionController()->addMessage(sprintf($this->getActionController()->plural('%s record updated due to sorting.', '%s records updated due to sorting.', $changed), $changed));

                return;
            }
        }

        throw new \Gems_Exception($this->_('Sorting failed'), 403);
    }
public function ajaxAction ( $ table , $ idField , $ orderField ) { $ request = $ this -> getRequest ( ) ; if ( $ request -> isPost ( ) ) { $ ids = $ request -> getPost ( 'ids' ) ; foreach ( $ ids as $ id ) { $ cleanIds [ ] = ( int ) $ id ; } $ db = $ this -> getActionController ( ) -> db ; $ select = $ db -> select ( ) -> from ( $ table , array ( $ idField , $ orderField ) ) -> where ( $ idField . ' in(?)' , $ cleanIds ) -> order ( $ orderField ) ; $ oldOrder = $ db -> fetchPairs ( $ select ) ; if ( count ( $ oldOrder ) == count ( $ cleanIds ) ) { $ newOrder = array_combine ( $ cleanIds , $ oldOrder ) ; $ changed = 0 ; foreach ( $ newOrder as $ id => $ order ) { $ changed = $ changed + $ db -> update ( $ table , array ( $ orderField => $ order ) , $ db -> quoteInto ( $ idField . ' = ?' , $ id ) ) ; } $ this -> getActionController ( ) -> addMessage ( sprintf ( $ this -> getActionController ( ) -> plural ( '%s record updated due to sorting.' , '%s records updated due to sorting.' , $ changed ) , $ changed ) ) ; return ; } } throw new \ Gems_Exception ( $ this -> _ ( 'Sorting failed' ) , 403 ) ; }
Handles sort in an ajax request

@param string $table The table used
@param string $idField The name of the field with the primary key
@param string $orderField The name of the field that holds the order
@return void
@throws Gems_Exception
Handles sort in an ajax request