Wednesday, 19 March 2014

How to store multiple jform fields under one field name in database in JSON formate

How to store multiple jform fields under one field name in database in JSON format.
For Instance,
if you want to store plan type(Forever, Fixed, Recurring...), plan price, Number of Recurring, Trial 1 time period, Trial 1 Price..etc information about plan into only one field name "details" into database, then
you can use below code for it.

create all required fields in your view's xml file.
As per my example you have to create fields in
joomla/administrator/components/com_your_component/models/forms/plan.xml(your view's xml file)

After creating all required fields.
Overrides the store() function of JTable class in  below file and add following code in it.
joomla/administrator/components/com_your_component/table/plan.php(your view's table file)

public function store($updateNulls = false)
{
    $input = JFactory::getApplication()->input;

    $plandata = $input->post->get('jform', array(), array());

    $details = array(

            'type'                 => $plandata['type'],

            'price'                => $plandata['price'],

            'expirationTime'       => $plandata['expirationTime'],

            'trial1price'          => $plandata['trial1price'],

            'trial1expirationTime' => $plandata['trial1expirationTime'],

            'trial2price'          => $plandata['trial2price'],

            'trial2expirationTime' => $plandata['trial2expirationTime'],

            'recurranceNumber'     => $plandata['recurranceNumber']
    );

    $detailString = new JRegistry($details);

    $this->details = $detailString->toString();

    // Store the row
    parent::store($updateNulls);

    return true;
}

In this I have stored all the array value in "details" field of my table in database in JSON format.

Tuesday, 4 March 2014

Create Search Tool in Joomla Component


Create a search tool in Joomla component.
create a file in administrator/component/com_your_component/models/forms/fileter_formname.xmlmake sure that published and access field name is same as your database field name for this code only.
If you are use different fields than their name are must same as your database field name.
In this code I have used only published and access fields of my database.
you can use as per your requirement.


<?xml version="1.0" encoding="utf-8"?>

<form>

<fields name="filter">

<field

name="search"

type="text"

label="COM_YOUR_COMPONENT_FILTER_SEARCH_DESC"

hint="JSEARCH_FILTER"

>

<field

name="published"

type="status"

label="COM_YOUR_COMPONENT_FILTER_PUBLISHED"

description="COM_YOUR_COMPONENT_FILTER_PUBLISHED_DESC"

onchange="this.form.submit();"

>

<option value=>JOPTION_SELECT_PUBLISHED</option>

</field>

<field

name="access"

type="accesslevel"

label="JOPTION_FILTER_ACCESS"

description="JOPTION_FILTER_ACCESS_DESC"

onchange="this.form.submit();"

>

<option value=>JOPTION_SELECT_ACCESS</option>

</field>

</fields>

<fields name="list">

<field

name="fullordering"

type="list"

label="COM_YOUR_COMPONENT_LIST_FULL_ORDERING"

description="COM_YOUR_COMPONENT_LIST_FULL_ORDERING_DESC"

onchange="this.form.submit();"

default="c.title ASC"

>

<option value=>JGLOBAL_SORT_BY</option>

<option value="c.published ASC">JSTATUS_ASC</option>

<option value="c.published DESC">JSTATUS_DESC</option>

<option value="c.title ASC">JGLOBAL_TITLE_ASC</option>

<option value="c.title DESC">JGLOBAL_TITLE_DESC</option>

<option value="c.access ASC">JGRID_HEADING_ACCESS_ASC</option>

<option value="c.access DESC">JGRID_HEADING_ACCESS_DESC</option>

<option value="c.id ASC">JGRID_HEADING_ID_ASC</option>

<option value="c.id DESC">JGRID_HEADING_ID_DESC</option>

</field>

<field

name="limit"

type="limitbox"

class="inputbox input-mini"

default="25"

label="COM_YOUR_COMPONENT_LIST_LIMIT"

description="COM_YOUR_COMPONENT_LIST_LIMIT_DESC"

onchange="this.form.submit();"

>

</fields>



</form>



add below code in administrator/components/com_your_component/views/yourviewsfolder/tmpl/default.php

<form action="<?php echo JRoute::_('index.php?option=com_your_component&view=yourviews'); ?>" method="post" name="adminForm" id="adminForm">



<?php if (!empty( $this->sidebar)) : ?>

<div id="j-sidebar-container" class="span2">

<?php echo $this->sidebar; ?>

</div>

<div id="j-main-container" class="span10">

<?php else : ?>

<div id="j-main-container">

<?php endif;?>

<?php

/ Search tools bar

echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));

?>

<?php if (empty($this->items)) : ?>

<div class="alert alert-no-items">

<?php echo JText::_('JGLOBAL_NO_MATCHING_RESULTS'); ?>

</div>

<?php else : ?>

this code is creates search tool in your default view.

Saturday, 1 March 2014

Create Customize Filed in Joomla

This code is used to create a customize field which is used to upload a image and display it while creating a new article or product in your component.
It will display image when you edit any item from your component and you can upload other image.
create a file in administrator/components/com_your_component/models/filed/uploadimage.php

<?php
/**
 * @package     Joomla.Platform
 * @subpackage  Form
 *
 * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */
defined('JPATH_PLATFORM') or die;
jimport('joomla.image.image');

/**
 * Form Field class for the Joomla Framework.
 *
 * @since  0.0.6
 */
class JFormFieldUploadImage extends JFormField
{
    /**
     * The form field type.
     *
     * @var      string
     * @since   1.6
     */
    protected $type = 'UploadImage';
    /**
     * Method to get the field input markup.
     *
     * @return   string   The field input markup.
     *
     * @since   1.6
     */
    protected function getInput()
    {
        $html = array();
        $html[] = '<div class="button2-left">';
        $html[] = '<input type="file" name="' . $this->name . '"/>';
        $html[] = '</div>';
        $html[] = '<div>';
        $html[] = '<img  src="' . JUri::root() . 'media/com_your_component/images/category/                   ' . $this->value . '"/>';
        $html[] = '<input type="hidden" value="' . $this->value . '" name="jform[hiddenimag                     e]"/>';
        $html[] = '</div>';

       return implode("\n", $html);
    }
}

create a file in administrator/components/com_your_component/models/forms/uploadimage.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>
        <field name="image" 
               default=""
               type="UploadImage"
               hide_none="1 "
               label="COM_YOUR_COMPONENT_FIELD_PARAMS_IMAGE_LABEL"
               description="COM_YOUR_COMPONENT_FIELD_PARAMS_IMAGE_DESC"
               componentname="com_your_component"
         />
    </fieldset>
</form>

add enctype="multipart/form-data" attribute of the from in below file:

administrator/components/com_your_component/views/your_view/tmpl/edit.php
In administrator/component/com_your_component/models/your_model.php
write below code into save function
$table = $this->getTable();
$input = JFactory::getApplication()->input;
$pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
$isNew = true;
$hiddenImage = array();
$hiddenImage = $input->post->get('jform', array(), array());
// Retrieve file details from uploaded file, sent from upload form
$file = $input->files->get('jform');

// Clean up filename to get rid of strange characters like spaces etc
$data['image'] = time() . '_' . JFile::makeSafe($file['image']['name']);

// Set up the source and destination of the file
$src = $file['image']['tmp_name'];
$dest = JPATH_SITE . '/media/com_your_component/images/category/' . $data['image'];

// First check if the file has the right extension, we need jpg only
if (!empty($file['image']['name']))
{
    if (preg_match('/(.)+jpe*g|gif|png/i', $file['image']['name']))
    { 
        if (!JFile::upload($src, $dest) )
        {
            $this->setError(JText::_('Invalid Image Type'));
            return false;
        }
        else
        {
            unlink(JPATH_SITE . '/media/com_your_component/images/category/' . $hiddenImage['hiddenimage']);
         }
    }
}
elseif (!empty($hiddenImage['hiddenimage']))
{
    $data['image'] = JFile::makeSafe($hiddenImage['hiddenimage']);
}
else
{
    $this->setError(Jtext::_('Invalid Image Type Not upload condition..'));
    
    return false;
}