March 23, 2012

FuelPHP Advent Calendar 2011 の電子書籍

昨年に参加させて頂いたFuelPHP Advent Calendar 2011の電子書籍が、
技術評論社様より出版されました。
216ページ相当で無料です。

Gihyo Digital Publishingに会員登録すると、ダウンロード出来ます。
https://gihyo.jp/dp/sp/advent2011/G11C13

FuelPHP Advent Calendar 2011の内容を、ただ時系列順に並べた内容です。
なので、一般的な参考書のようなまとまりは有りません。
とは言うものの、FuelPHPという新しいフレームワークに
探りを入れるためのTIPは随所に散らばっていると思います。

2011年12月時点の内容なので、最新の情報というわけでは有りませんが、
無料なので、興味のある方は是非、読んでみて下さい。

March 17, 2012

FuelPHPのPaginationのajax版

FuelPHPのPaginationはページングのリンクをaリンクで出力します。
これを、ajaxで使いたいなーと思い、独自クラスで作成してみました。

標準のPaginationでは、Pagination::set_configに渡すconfig配列の
pagination_urlを元に、リンクを作成します。
http://docs.fuelphp.com/classes/pagination.html
pagination_urlの末尾に、ページ番号が加えられます。

今回作成したクラスは、pagination_urlの代わりに、onclickを指定します。
'onclick' => 'hoge'とすると、
<a href="#" onclick="hoge([ページ番号]);return false;">[ページ番号]</a>


といった感じのリンクを出力します。

onclickで呼び出すjavascriptの関数は、予めhtmlに記述しておきます。
function hoge(page) {
    // TODO:
}



以下、ソースです。
coreのPaginationクラスをコピーして、部分的に修正しています。
<?php
class My_Pagination
{

    /**
     * @var    integer    The current page
     */
    public static $current_page = null;

    /**
     * @var    integer    The offset that the current page starts at
     */
    public static $offset = 0;

    /**
     * @var    integer    The number of items per page
     */
    public static $per_page = 10;

    /**
     * @var    integer    The number of total pages
     */
    public static $total_pages = 0;

    /**
     * @var array The HTML for the display
     */
    public static $template = array(
        'wrapper_start'  => '',
        'page_start'     => ' ',
        'page_end'       => ' ',
        'previous_start' => ' ',
        'previous_end'   => ' ',
        'previous_mark'  => '« ',
        'next_start'     => ' ',
        'next_end'       => ' ',
        'next_mark'      => ' »',
        'active_start'   => ' ',
        'active_end'     => ' ',
    );

    /**
     * @var    integer    The total number of items
     */
    protected static $total_items = 0;

    /**
     * @var    integer    The total number of links to show
     */
    protected static $num_links = 5;

    /**
     * @var    integer    The URI segment containg page number
     */
    protected static $uri_segment = 3;

    /**
     * @var mixed   The pagination onclick
     */
    protected static $onclick;

    /**
     * Init
     *
     * Loads in the config and sets the variables
     *
     * @access    public
     * @return    void
     */
    public static function _init()
    {
        $config = \Config::get('pagination', array());

        static::set_config($config);
    }

    // --------------------------------------------------------------------

    /**
     * Set Config
     *
     * Sets the configuration for pagination
     *
     * @access public
     * @param array   $config The configuration array
     * @return void
     */
    public static function set_config(array $config)
    {

        foreach ($config as $key => $value)
        {
            if ($key == 'template')
            {
                static::$template = array_merge(static::$template, $config['template']);
                continue;
            }

            static::${$key} = $value;
        }

        static::initialize();
    }

    // --------------------------------------------------------------------

    /**
     * Prepares vars for creating links
     *
     * @access public
     * @return array    The pagination variables
     */
    protected static function initialize()
    {
        static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1;

        static::$current_page = (int) \URI::segment(static::$uri_segment);

        if (static::$current_page > static::$total_pages)
        {
            static::$current_page = static::$total_pages;
        }
        elseif (static::$current_page < 1)
        {
            static::$current_page = 1;
        }

        // The current page must be zero based so that the offset for page 1 is 0.
        static::$offset = (static::$current_page - 1) * static::$per_page;
    }

    // --------------------------------------------------------------------

    /**
     * Creates the pagination links
     *
     * @access public
     * @return mixed    The pagination links
     */
    public static function create_links()
    {
        if (static::$total_pages == 1)
        {
            return '';
        }

        \Lang::load('pagination', true);

        $pagination  = static::$template['wrapper_start'];
        $pagination .= static::prev_link(\Lang::get('pagination.previous'));
        $pagination .= static::page_links();
        $pagination .= static::next_link(\Lang::get('pagination.next'));
        $pagination .= static::$template['wrapper_end'];

        return $pagination;
    }

    // --------------------------------------------------------------------

    /**
     * Pagination Page Number links
     *
     * @access public
     * @return mixed    Markup for page number links
     */
    public static function page_links()
    {
        if (static::$total_pages == 1)
        {
            return '';
        }

        $pagination = '';

        // Let's get the starting page number, this is determined using num_links
        $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1;

        // Let's get the ending page number
        $end   = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages;

        for($i = $start; $i <= $end; $i++)
        {
            if (static::$current_page == $i)
            {
                $pagination .= static::$template['active_start'].$i.static::$template['active_end'];
            }
            else
            {
                $pagination .= \Html::anchor('#', $i, array('onclick' => static::$onclick .'('.$i.');return false;'));
            }
        }

        return static::$template['page_start'].$pagination.static::$template['page_end'];
    }

    // --------------------------------------------------------------------

    /**
     * Pagination "Next" link
     *
     * @access public
     * @param string $value The text displayed in link
     * @return mixed    The next link
     */
    public static function next_link($value)
    {
        if (static::$total_pages == 1)
        {
            return '';
        }

        if (static::$current_page == static::$total_pages)
        {
            return $value.static::$template['next_mark'];
        }
        else
        {
            $next_page = static::$current_page + 1;
            return \Html::anchor('#', $value.static::$template['next_mark'], array('onclick' => static::$onclick .'('.$next_page.');return false;'));
        }
    }

    // --------------------------------------------------------------------

    /**
     * Pagination "Previous" link
     *
     * @access public
     * @param string $value The text displayed in link
     * @return mixed    The previous link
     */
    public static function prev_link($value)
    {
        if (static::$total_pages == 1)
        {
            return '';
        }

        if (static::$current_page == 1)
        {
            return static::$template['previous_mark'].$value;
        }
        else
        {
            $previous_page = static::$current_page - 1;
            return \Html::anchor('#', static::$template['previous_mark'].$value, array('onclick' => static::$onclick .'('.$previous_page.');return false;'));
        }
    }
}

March 10, 2012

複数のGitHubアカウントを使用する際のメモ。

~/.ssh/configの内容
--
Host github.com-1
User git
Hostname github.com
IdentityFile xxx/id_rsa

Host github.com-2
User git
Hostname github.com
IdentityFile yyy/id_rsa
--

それぞれ、以下のような形でremoteをaddする。
git remote add origin git@github.com-1:XXX/test.git
git remote add origin git@github.com-2:YYY/test.git