Pages

October 27, 2010

AutoComplete Codeigniter สำหรับ Netbeans

เป็น autocomplete สำหรับ codeigniter 1.7 ใช้ใน netbeans 6.7+ (กล่าวตามที่มา)

ขั้นตอนในการติดตั้ง 
ทำการคัดลอก php code ด้านล่างนี้แล้วสร้างไฟล์ชื่อ auto_complete.php ( ชื่อเป็นอะไรก็ได้ ขอแค่นามสกุลเป็น php ) จากนั้นนำไปวางในโฟลเดอร์ netbeans ( หรือที่อื่นก็ได้ ) ดังรูป แค่นี้เราก็ได้ระบบ autoComplete สำหรับ Codeigniter ใช้ใน netbeans แล้ว 
 
แสดงไฟล์ autoComplete ที่วางในโฟลเดอร์ netbeans

ตัวอย่างการใช้งาน autoComplete ใน Controller

แบบพื้นฐาน
/**
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Benchmark $benchmark
* @property CI_Calendar $calendar
* @property CI_Cart $cart
* @property CI_Config $config
* @property CI_Controller $controller
* @property CI_Email $email
* @property CI_Encrypt $encrypt
* @property CI_Exceptions $exceptions
* @property CI_Form_validation $form_validation
* @property CI_Ftp $ftp
* @property CI_Hooks $hooks
* @property CI_Image_lib $image_lib
* @property CI_Input $input
* @property CI_Language $language
* @property CI_Loader $load
* @property CI_Log $log
* @property CI_Model $model
* @property CI_Output $output
* @property CI_Pagination $pagination
* @property CI_Parser $parser
* @property CI_Profiler $profiler
* @property CI_Router $router
* @property CI_Session $session
* @property CI_Sha1 $sha1
* @property CI_Table $table
* @property CI_Trackback $trackback
* @property CI_Typography $typography
* @property CI_Unit_test $unit_test
* @property CI_Upload $upload
* @property CI_URI $uri
* @property CI_User_agent $user_agent
* @property CI_Validation $validation
* @property CI_Xmlrpc $xmlrpc
* @property CI_Xmlrpcs $xmlrpcs
* @property CI_Zip $zip
*/

class Controller {};

/**
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Config $config
* @property CI_Loader $load
* @property CI_Session $session
*/

class Model {};
แบบครบถ้วน
/**
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Benchmark $benchmark
* @property CI_Calendar $calendar
* @property CI_Cart $cart
* @property CI_Config $config
* @property CI_Controller $controller
* @property CI_Email $email
* @property CI_Encrypt $encrypt
* @property CI_Exceptions $exceptions
* @property CI_Form_validation $form_validation
* @property CI_Ftp $ftp
* @property CI_Hooks $hooks
* @property CI_Image_lib $image_lib
* @property CI_Input $input
* @property CI_Language $language
* @property CI_Loader $load
* @property CI_Log $log
* @property CI_Model $model
* @property CI_Output $output
* @property CI_Pagination $pagination
* @property CI_Parser $parser
* @property CI_Profiler $profiler
* @property CI_Router $router
* @property CI_Session $session
* @property CI_Sha1 $sha1
* @property CI_Table $table
* @property CI_Trackback $trackback
* @property CI_Typography $typography
* @property CI_Unit_test $unit_test
* @property CI_Upload $upload
* @property CI_URI $uri
* @property CI_User_agent $user_agent
* @property CI_Validation $validation
* @property CI_Xmlrpc $xmlrpc
* @property CI_Xmlrpcs $xmlrpcs
* @property CI_Zip $zip
* @property Image_Upload $image_upload
* @property Lang_Detect $lang_detect
* @property Address_Model $address_model
* @property Admin_Model $admin_model
* @property Buyer_Model $buyer_model
* @property Email_Model $email_model
* @property Product_Model $product_model
* @property Store_Model $store_model
* @property Tailor_Model $tailor_model
*/

class Controller {};

/**
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Config $config
* @property CI_Loader $load
* @property CI_Session $session
* @property Address_Model $address_model
* @property Admin_Model $admin_model
* @property Buyer_Model $buyer_model
* @property Email_Model $email_model
* @property Product_Model $product_model
* @property Store_Model $store_model
* @property Tailor_Model $tailor_model
*/

class Model {};
ฟังก์ชั่น php สำหรับสร้าง php code ที่ใช้ในการทำ autoComplete 
( นำไปวางใน Controller จากนั้นเข้าที่ฟังก์ชั่นนั้น ตัวฟังก์ชั่นจะทำการ Generate ให้อัตโนมัติ ข้อดีคือมันจะเอา Models ที่เราสร้างมาทำเป็น autoComplete ให้ด้วย )
function autocomplete() {
    $sys_lib = BASEPATH . '/libraries';
    $app_lib = APPPATH . '/libraries';
    $app_model = APPPATH . '/models';

    echo "<?php<br/><br/>";
    echo "/**<br/>";
    echo '* @property CI_DB_active_record $db<br/>';
    echo '* @property CI_DB_forge $dbforge<br/>';

    if ($handle = opendir($sys_lib)) {
        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle))) {
            if ($file[0] == '.')
                continue;
            $files = explode('.', $file);
            $file = $files[0];
            $file2 = $file;
            if ($file == 'index')
                continue;
            if ($file == 'Loader')
                $file2 = 'load';
            echo "* @property CI_" . $file . " $" . strtolower($file2) . "<br/>";
        }
        closedir($handle);
    }
    if ($handle = opendir($app_lib)) {
        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle))) {
            if ($file[0] == '.')
                continue;
            $files = explode('.', $file);
            $file = $files[0];
            $file_parts = explode('_', $file);
            $first_part = $file_parts[0];
            if ($first_part == 'index' || $first_part == 'MY')
                continue;
            if (count($file_parts) > 1) {
                $last_part = $file_parts[1];
                echo "* @property " . ucfirst($first_part) . "_" . ucfirst($last_part) . " $" . strtolower($first_part) . "_" . strtolower($last_part) . "<br/>";
            } else {
                echo "* @property " . ucfirst($first_part) . " $" . strtolower($first_part) . "<br/>";
            }
        }
        closedir($handle);
    }
    if ($handle = opendir($app_model)) {
        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle))) {
            if ($file[0] == '.')
                continue;
            $files = explode('.', $file);
            $file = $files[0];
            if ($file == 'index')
                continue;
            $file_parts = explode('_', $file);
            $first_part = $file_parts[0];
            $last_part = $file_parts[1];
            echo "* @property " . ucfirst($first_part) . "_" . ucfirst($last_part) . " $" . strtolower($first_part) . "_" . strtolower($last_part) . "<br/>";
        }
        closedir($handle);
    }
    echo "*/<br/><br/>";
    echo "class Controller {}<br/><br/>";
    echo "?>";
}
จบแล้วครับ ขอบคุณครับ
Credits : http://rhasan.com/blog/2009/09/codeigniter-auto-complete-with-netbeans/

1 comment: