สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

หมวดสำหรับแบ่งบันความ รู้ต่างๆ จะมีหมวดย่อยๆ ในหมวดนี้ เช่น php, SQL, XML, CSS

Moderators: mindphp, ผู้ดูแลกระดาน

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 11752
Joined: 18/04/2012 9:39 am

สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

Post by tsukasaz »

ในโฟลเดอร์ admin ให้สร้างไฟล์ config.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<config>
	<fieldset
		name="greetings"
		label="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL"
		description="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC"
	>
		<field
			name="show_category"
			type="radio"
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
			default="0"
		>
			<option value="0">JHIDE</option>
			<option value="1">JSHOW</option>
		</field>
	</fieldset>
</config>
เสร็จแล้วเข้าไปที่โฟลเดอร์ views -> helloworlds เปิดไฟล์ view.html.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HelloWorlds View
 */
class HelloWorldViewHelloWorlds extends JView
{
    /**
     * HelloWorlds view display method
     * @return void
     */
    function display($tpl = null) 
    {
        // Get data from the model
        $items = $this->get('Items');
        $pagination = $this->get('Pagination');
 
        // Check for errors.
        if (count($errors = $this->get('Errors'))) 
        {
            JError::raiseError(500, implode('<br />', $errors));
            return false;
        }
        // Assign data to the view
        $this->items = $items;
        $this->pagination = $pagination;
 
        // Set the toolbar
        $this->addToolBar();
 
        // Display the template
        parent::display($tpl);
 
        // Set the document
        $this->setDocument();
    }
 
    /**
     * Setting the toolbar
     */
    protected function addToolBar() 
    {
        JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'), 'helloworld');
        JToolBarHelper::deleteListX('', 'helloworlds.delete');
        JToolBarHelper::editListX('helloworld.edit');
        JToolBarHelper::addNewX('helloworld.add');
        JToolBarHelper::preferences('com_helloworld');
    }
    /**
     * Method to set up the document properties
     *
     * @return void
     */
    protected function setDocument() 
    {
        $document = JFactory::getDocument();
        $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
    }
} 
เข้าไปที่โฟลเดอร์ admin -> models -> forms เปิดไฟล์ helloworld.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form
	addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<fieldset name="details">
		<field
			name="id"
			type="hidden"
		/>
		<field
			name="greeting"
			type="text"
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
			size="40"
			class="inputbox validate-greeting"
			validate="greeting"
			required="true"
			default=""
		/>
		<field
			name="catid"
			type="category"
			extension="com_helloworld"
			class="inputbox"
			default=""
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
			required="true"
		>
			<option value="0">JOPTION_SELECT_CATEGORY</option>
		</field>
	</fieldset>
	<fields name="params">
		<fieldset
			name="params"
			label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS"
		>
			<field
				name="show_category"
				type="list"
				label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
				description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
				default=""
			>
				<option value="">JGLOBAL_USE_GLOBAL</option>
				<option value="0">JHIDE</option>
				<option value="1">JSHOW</option>
			</field>
		</fieldset>
	</fields>
</form>
เข้าไปที่โฟลเดอร์ admin -> sql เปิดไฟล์ install.mysql.utf8.sql แก้โค้ด

Code: Select all

DROP TABLE IF EXISTS `#__helloworld`;
 
CREATE TABLE `#__helloworld` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `greeting` varchar(25) NOT NULL,
  `catid` int(11) NOT NULL DEFAULT '0',
  `params` TEXT NOT NULL DEFAULT '',
   PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
 
INSERT INTO `#__helloworld` (`greeting`) VALUES
        ('Hello World!'),
        ('Good bye World!');
เข้าไปในโฟลเดอร์ updates -> mysql สร้างไฟล์ 0.0.13.sql

Code: Select all

ALTER TABLE `#__helloworld` ADD `params` VARCHAR(1024) NOT NULL DEFAULT '';
เข้าไปที่โฟลเดอร์ admin -> tables เปิดไฟล์ helloworld.php

Code: Select all

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
 
// import Joomla table library
jimport('joomla.database.table');
 
/**
 * Hello Table class
 */
class HelloWorldTableHelloWorld extends JTable
{
    /**
     * Constructor
     *
     * @param object Database connector object
     */
    function __construct(&$db) 
    {
        parent::__construct('#__helloworld', 'id', $db);
    }
    /**
     * Overloaded bind function
     *
     * @param       array           named array
     * @return      null|string     null is operation was satisfactory, otherwise returns an error
     * @see JTable:bind
     * @since 1.5
     */
    public function bind($array, $ignore = '') 
    {
        if (isset($array['params']) && is_array($array['params'])) 
        {
            // Convert the params field to a string.
            $parameter = new JRegistry;
            $parameter->loadArray($array['params']);
            $array['params'] = (string)$parameter;
        }
        return parent::bind($array, $ignore);
    }
 
    /**
     * Overloaded load function
     *
     * @param       int $pk primary key
     * @param       boolean $reset reset data
     * @return      boolean
     * @see JTable:load
     */
    public function load($pk = null, $reset = true) 
    {
        if (parent::load($pk, $reset)) 
        {
            // Convert the params field to a registry.
            $params = new JRegistry;                 
                       // loadJSON is @deprecated    12.1  Use loadString passing JSON as the format instead.
                       // $params->loadString($this->item->params, 'JSON');
                       // "item" should not be present.
                       $params->loadJSON($this->params);
 
            $this->params = $params;
            return true;
        }
        else
        {
            return false;
        }
    }
} 
เข้าไปที่โฟลเดอร์ admin -> views -> helloworld -> tmpl เปิดไฟล์ edit.php

Code: Select all

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
$params = $this->form->getFieldsets('params');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this->item->id); ?>"
      method="post" name="adminForm" id="helloworld-form" class="form-validate">
 
    <div class="width-60 fltlft">
        <fieldset class="adminform">
            <legend><?php echo JText::_( 'COM_HELLOWORLD_HELLOWORLD_DETAILS' ); ?></legend>
            <ul class="adminformlist">
<?php                      foreach($this->form->getFieldset('details') as $field): ?>
                <li><?php echo $field->label;echo $field->input;?></li>
<?php                      endforeach; ?>
            </ul>
        </fieldset>
    </div>
 
    <div class="width-40 fltrt">
<?php        echo JHtml::_('sliders.start', 'helloworld-slider');
             foreach ($params as $name => $fieldset):
                echo JHtml::_('sliders.panel', JText::_($fieldset->label), $name.'-params');
                if (isset($fieldset->description) && trim($fieldset->description)): ?>
                    <p class="tip"><?php echo $this->escape(JText::_($fieldset->description));?></p>
<?php           endif;?>
        <fieldset class="panelform" >
            <ul class="adminformlist">
<?php                        foreach ($this->form->getFieldset($name) as $field) : ?>
                <li><?php echo $field->label; ?><?php echo $field->input; ?></li>
<?php                        endforeach; ?>
            </ul>
        </fieldset>
<?php        endforeach; ?>
 
        <?php echo JHtml::_('sliders.end'); ?>
    </div>
 
    <div>
        <input type="hidden" name="task" value="helloworld.edit" />
        <?php echo JHtml::_('form.token'); ?>
    </div>
</form>
Last edited by tsukasaz on 11/05/2012 4:31 pm, edited 1 time in total.
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 11752
Joined: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

Post by tsukasaz »

มาที่โฟลเดอร์ site -> models -> helloworld.php แก้โค้ด

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
 
/**
 * HelloWorld Model
 */
class HelloWorldModelHelloWorld extends JModelItem
{
    /**
     * @var object item
     */
    protected $item;
 
    /**
     * Method to auto-populate the model state.
     *
     * This method should only be called once per instantiation and is designed
     * to be called on the first call to the getState() method unless the model
     * configuration flag to ignore the request is set.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @return    void
     * @since    2.5
     */
    protected function populateState() 
    {
        $app = JFactory::getApplication();
        // Get the message id
        $id = JRequest::getInt('id');
        $this->setState('message.id', $id);
 
        // Load the parameters.
        $params = $app->getParams();
        $this->setState('params', $params);
        parent::populateState();
    }
 
    /**
     * Returns a reference to the a Table object, always creating it.
     *
     * @param    type    The table type to instantiate
     * @param    string    A prefix for the table class name. Optional.
     * @param    array    Configuration array for model. Optional.
     * @return    JTable    A database object
     * @since    2.5
     */
    public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array()) 
    {
        return JTable::getInstance($type, $prefix, $config);
    }
 
    /**
     * Get the message
     * @return object The message to be displayed to the user
     */
    public function getItem() 
    {
        if (!isset($this->item)) 
        {
            $id = $this->getState('message.id');
            $this->_db->setQuery($this->_db->getQuery(true)
                ->from('#__helloworld as h')
                ->leftJoin('#__categories as c ON h.catid=c.id')
                ->select('h.greeting, h.params, c.title as category')
                ->where('h.id=' . (int)$id));
            if (!$this->item = $this->_db->loadObject()) 
            {
                $this->setError($this->_db->getError());
            }
            else
            {
                // Load the JSON string
                $params = new JRegistry;
                                // loadJSON is @deprecated    12.1  Use loadString passing JSON as the format instead.
                //$params->loadString($this->item->params, 'JSON');
                                $params->loadJSON($this->item->params);
                $this->item->params = $params;
 
                // Merge global params with item params
                $params = clone $this->getState('params');
                $params->merge($this->item->params);
                $this->item->params = $params;
            }
        }
        return $this->item;
    }
} 
เข้าที่โฟลเดอร์ site -> views -> helloworld แก้ไขไฟล์ view.html.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 */
class HelloWorldViewHelloWorld extends JView
{
    // Overwriting JView display method
    function display($tpl = null) 
    {
        // Assign data to the view
        $this->item = $this->get('Item');
 
        // Check for errors.
        if (count($errors = $this->get('Errors'))) 
        {
            JError::raiseError(500, implode('<br />', $errors));
            return false;
        }
        // Display the view
        parent::display($tpl);
    }
} 
เข้าไปในโฟลเดอร์ tmpl แก้ไขไฟล์ default.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->item->greeting.(($this->item->category and $this->item->params->get('show_category'))
                                      ? (' ('.$this->item->category.')') : ''); ?>
</h1>
กลับไปที่โฟลเดอร์ admin -> language -> en-GB แก้ไขไฟล์ en-GB.com_helloworld.ini

Code: Select all

COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_ADMINISTRATION_CATEGORIES="HelloWorld - Categories"
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Some values are unacceptable"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="The category the messages belongs to"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL="Show category"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC="If set to Show, the title of the message&rsquo;s category will show."
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="Id"
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"
COM_HELLOWORLD_SUBMENU_MESSAGES="Messages"
COM_HELLOWORLD_SUBMENU_CATEGORIES="Categories"
COM_HELLOWORLD_CONFIGURATION="HelloWorld Configuration"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL="Messages settings"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC="Settings that will be applied to all messages by default"
เปิดไฟล์ helloworld.xml แก้ไขใหม่

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
 
	<name>COM_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>November 2009</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.13</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_HELLOWORLD_DESCRIPTION</description>
 
	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New in 2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>
 
	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
		<folder>language</folder>
	</files>
 
	<media destination="com_helloworld" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>
 
	<administration>
		<!-- Administration Menu Section -->
		<menu img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>config.xml</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
			<!-- controllers files section -->
			<folder>controllers</folder>
			<!-- helpers files section -->
			<folder>helpers</folder>
		</files>
 
		<languages folder="admin">
			<language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
			<language tag="en-GB">language/en-GB/en-GB.com_helloworld.sys.ini</language>
		</languages>
	</administration>
 
</extension>
ทำเป็น .zip แล้ว เข้าไปหน้าของ administrator ไปเพิ่ม extensions เข้าไปใหม่
Last edited by tsukasaz on 11/05/2012 4:30 pm, edited 1 time in total.
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 11752
Joined: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

Post by tsukasaz »

มาดูที่ Component Hello World สังเกตุว่าเครื่องด้านขวามือจะมี Options เพิ่มเข้ามา
Attachments
config1 (Small).JPG
config1 (Small).JPG (26.27 KiB) Viewed 2635 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 11752
Joined: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

Post by tsukasaz »

เมื่อคลิกเข้าไปในส่วนของ Options
Attachments
config2 (Small).JPG
config2 (Small).JPG (15.57 KiB) Viewed 2634 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 11752
Joined: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 การสร้าง Configuration

Post by tsukasaz »

ในหน้าแก้ไขข้อมูล
Attachments
config3 (Small).JPG
config3 (Small).JPG (25.3 KiB) Viewed 2634 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Share Knowledge”

Who is online

Users browsing this forum: No registered users and 9 guests