[分享]修正 cakephp2 在 PaginatorHelper->sort() 調整參數位置問題

edited 十月 2013 in CakePHP
簡單的說,原本是這樣:
$this->Paginator->sort('名稱', 'title');
現在要改為這樣:
$this->Paginator->sort('title', '名稱');

因為使用的數量太多,所以寫了一個指令程式去改:
<?php
App::import('Core', 'Folder');
class Cake2Shell extends Shell {

    function main() {
        $fh = new Folder(VIEWS);
        $files = $fh->findRecursive();
        foreach($files AS $file) {
            $content = file_get_contents($file);
            $offset = 0;
            $newContent = '';
            while(FALSE !== $pos = strpos($content, '$this->Paginator->sort(', $offset)) {
                $newContent .= substr($content, $offset, $pos - $offset);
                $posEnd = strpos($content, ')', $pos) + 1;
                $target = substr($content, $pos, $posEnd - $pos);
                $part1Pos = strpos($target, "'");
                $part1End = strpos($target, "'", $part1Pos + 1) + 1;
                $part1Length = $part1End - $part1Pos;
                $part2Pos = strpos($target, "'", $part1Pos + $part1Length);
                $part2End = strpos($target, "'", $part2Pos + 1) + 1;
                $part2Length = $part2End - $part2Pos;
                $target = implode('', array(
                    substr($target, 0, $part1Pos),
                    substr($target, $part2Pos, $part2Length),
                    substr($target, $part1End, ($part2Pos - $part1End)),
                    substr($target, $part1Pos, $part1Length),
                    substr($target, $part2End),
                ));
                $newContent .= $target;
                $offset = $posEnd;
            }
            $newContent .= substr($content, $offset);
            file_put_contents($file, $newContent);
        }
    }

}

放在 vendors/shells/cake2.php ,然後執行它,像這樣:
/home/kiang/public_html/cake2/cake/console/cake cake2 -app /home/kiang/public_html/travel/

使用的前題是整個專案的習慣一致,如果不一致就自己想辦法加例外判斷吧 ;)

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=5677

評論

Sign In or Register to comment.