czwartek, 16 września 2010

Sequence Plugin

Very nice plugin for keeping table elements in order has been presented here
http://www.neilcrookes.com/2009/02/09/cakephp-sequence-behavior/
To fix the problem with 0-position element movement i slightly changed jquery script provided by Neil

<script type="text/javascript">
$(document).ready(function() {
  $("div.index table ").tableDnD({
    url: "<?php echo Router::url(array('action' => 'save_order')); ?>",
    originalOrder: null,
    onDragClass: "myDragClass",
    onDragStart: function(table, row) {
//        alert(originalOrder);
      originalOrder = jQuery.inArray(row, $(".dragMe", table));
    },
    onDrop: function(table, row) {
      var newOrder = jQuery.inArray(row, $(".dragMe", table));
      if (newOrder != originalOrder) {
        $.post(this.url+'/'+row.id, { 'data[<?php echo Inflector::singularize($this->name); ?>][weight]': newOrder });
      }
    },
  });
});
</script>
and little change in baked index function (just after foreach):
        $class = ' class="dragMe"';
        if ($i++ % 2 == 0) {
            $class = ' class="altrow dragMe"';
        }

saveAll -> saving deep associations

Currently cake doesn't allow saving deep models associations. For example you wan't be able to save in one form (let's assume user/add) User->Article->Tag.

środa, 15 września 2010

Plugin model associations

when defining models relation in plugin, remember to set className as 'pluginName'.'className'

ExtJS + tree behaviour = categories sorting

http://blogs.bigfish.tv/adam/2008/02/12/drag-and-drop-using-ext-js-with-the-cakephp-tree-behavior/
Note:
All root level categories needs to have parent_id set to NULL


Code:
    function reorder(){
   
    // retrieve the node instructions from javascript
    // delta is the difference in position (1 = next node, -1 = previous node)
    $node = intval($this->params['form']['node']);
    $delta = intval($this->params['form']['delta']);
    if ($delta > 0) {
        $this->Category->movedown($node, abs($delta));
    } elseif ($delta < 0) {
        $this->Category->moveup($node, abs($delta));
    }
    // send success response
    exit('1');
   
}
    /*do sortowania*/
    function reparent(){
   
    $node = intval($this->params['form']['node']);
    $parent = intval($this->params['form']['parent']);
    $position = intval($this->params['form']['position']);
   
    // save the employee node with the new parent id
    // this will move the employee node to the bottom of the parent list
   
    $this->Category->id = $node;
    $this->Category->saveField('parent_id', $parent);
   
    // If position == 0, then we move it straight to the top
    // otherwise we calculate the distance to move ($delta).
    // We have to check if $delta > 0 before moving due to a bug
    // in the tree behavior (https://trac.cakephp.org/ticket/4037)
   
    if ($position == 0){
        $this->Category->moveup($node, true);
    } else {
        $count = $this->Category->childcount($parent, true);
        $delta = $count-$position-1;
        if ($delta > 0){
            $this->Category->moveup($node, $delta);
        }
    }
   
    // send success response
    exit('1');
    }

    function sort(){
        $this->layout = 'extjs';
        $this->set('categories', $this->paginate());
        $stuff = $this->Category->find('all',
        array('fields' => array('id','name', 'lft', 'rght', 'parent_id'), 'order' => 'lft ASC'));
        $this->set('stuff', $stuff);
//        debug($stuff);
    }
    /*do sortowania*/
    function getnodes(){
        $this->layout = 'ajax';
        // retrieve the node id that Ext JS posts via ajax
        $parent = intval($this->params['form']['node']);
        $this->log('getNodes: '.$parent, LOG_DEBUG);

        // find all the nodes underneath the parent node defined above
        // the second parameter (true) means we only want direct children
        $nodes = $this->Category->children($parent, true);
        $this->log('getNodes: NODES: '.var_export($nodes,true), LOG_DEBUG);
//        debug($nodes);

        // send the nodes to our view
        $this->set(compact('nodes'));

    }

    function reorderCategories($id = null){
        if ($id === null && $this->Category->id) {
            $id = $this->Category->id;
        } elseif (!$id) {
            $id = null;
        }
        $this->Category->id = null;
//        $res = $this->Category->reorder(array('id'=>null,  'field'=> 'lft','order'=>'ASC'));
//        $this->Category->reorder(array('field' => 'Category.lft', 'order' =>'ASC'));
        $this->Category->reorder();
    }

needed Files:
categories/view/getnodes.ctp

niedziela, 12 września 2010

content sort

I'm going to check how plugin presented on http://www.neilcrookes.com/2009/02/09/cakephp-sequence-behavior/ works. As i saw the working example it's pretty much what i was looking for.

CKeditor/CKfinder

http://blog.beamstyle.com.hk/tutorial-on-integrating-ckfinderckeditor-into-cakephp-with-session-authentication/
This solution worked pritty well for me but i had to change setting up CKFinder for editor
to something like below.

CKFinder.setupCKEditor( null, { basePath : '<?echo $this->webroot;?>/js/ckfinder/'}) ;
  var ck_newsContent = CKEDITOR.replace('long');