Saturday, August 28, 2010

joomla add edit delete record lesson 5

Ok today i am going to explain three tasks. Add / edit or delete records from joomla admin panel. we discussed about 'form' on last lesson. To do the management of above three task, use following files

1.com_lanka1->views->lanka1->tmpl->form.php
2.com_lanka1->views->lanka1->view.html.php
3.com_lanka1->controllers->lanka.php
4.com_lanka1->models->lanka1.php
5.com_lanka1->tables->lanka1.php



let's review on by one

1.com_lanka1->views->lanka1->tmpl->form.php



<?php defined('_JEXEC') or die('Restricted access'); ?>
<?php
/**
* Lanka Map default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/
?>

<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col100">
<fieldset class="adminform">
<legend><?php echo JText::_( 'Details' ); ?></legend>

<table class="admintable">

<tr>
<td width="100" align="right" class="key">
<label for="greeting">
<?php echo JText::_( 'Your name'); ?>:
</label>
</td>
<td>
<input class="text_area" type="text" name="your_name" id="your_name" size="32" maxlength="250" value="<?php echo $this->Lanka->your_name;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="greeting">
<?php echo JText::_( 'Your email'); ?>:
</label>
</td>
<td>
<input class="text_area" type="text" name="your_email" id="your_email" size="32" maxlength="250" value="<?php echo $this->Lanka->your_email;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="greeting">
<?php echo JText::_( 'Your TP'); ?>:
</label>
</td>
<td>
<input class="text_area" type="text" name="your_tp" id="your_tp" size="32" maxlength="250" value="<?php echo $this->Lanka->your_tp;?>" />
</td>
</tr>
</table>
</fieldset>
</div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_lanka1" />
<input type="hidden" name="id" value="<?php echo $this->Lanka->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="lanka" />
</form>







2.com_lanka1->views->lanka1->view.html.php



<?php
/**
* Lanka1 default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view' );

/**
* tp View
*
* @package Joomla.Tutorials
* @subpackage Components
*/

class Lanka1sViewLanka1 extends JView
{
/**
* display method of Hello view
* @return void
**/
function display($tpl = null)
{
//get the tp
$Lanka =& $this->get('Data');

$isNew = ($Lanka->id < 1);

$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
JToolBarHelper::title( JText::_( 'Lanka1 ' ).': <small><small>[ ' . $text.' ]</small></small>' );
JToolBarHelper::save();
if ($isNew) {
JToolBarHelper::cancel();
} else {
// for existing items the button is renamed `close`
JToolBarHelper::cancel( 'cancel', 'Close' );
}

$this->assignRef('Lanka', $Lanka);

parent::display($tpl);
}
}





3.com_lanka1->controllers->lanka.php




<?php
/**
* Lanka1 default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/


// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
* Lanka1 Controller
*
* @package Joomla.component
* @subpackage Components
*/
class Lanka1sControllerLanka extends Lanka1sController
{
/**
* constructor (registers additional tasks to methods)
* @return void
*/
function __construct()
{
parent::__construct();

// Register Extra tasks
$this->registerTask( 'add' , 'edit' );
}

/**
* display the edit form
* @return void
*/
function edit()
{

JRequest::setVar( 'view', 'Lanka1' );
JRequest::setVar( 'layout', 'form' );
JRequest::setVar('hidemainmenu', 1);

parent::display();
}

/**
* save a record (and redirect to main page)
* @return void
*/
function save()
{
$model = $this->getModel('Lanka1');

if ($model->store($post)) {
$msg = JText::_( 'Lanka1 Saved!' );
} else {
$msg = JText::_( 'Error Saving Lanka1 ' );
}

// Check the table in so it can be edited.... we are done with it anyway
$link = 'index.php?option=com_Lanka1';
$this->setRedirect($link, $msg);
}

/**
* remove record(s)
* @return void
*/
function remove()
{
$model = $this->getModel('Lanka1');
if(!$model->delete()) {
$msg = JText::_( 'Error: One or More Lanka1 map Could not be Deleted' );
} else {
$msg = JText::_( ' Lanka1 Deleted' );
}

$this->setRedirect( 'index.php?option=com_Lanka1', $msg );
}

/**
* cancel editing a record
* @return void
*/
function cancel()
{
$msg = JText::_( 'Operation Cancelled' );
$this->setRedirect( 'index.php?option=com_Lanka1', $msg );
}
}




4.com_lanka1->models->lanka1.php



<?php
/**
* Lanka1 Map default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');


class Lanka1sModelLanka1 extends JModel
{
/**
* Constructor that retrieves the ID from the request
*
* @access public
* @return void
*/
function __construct()
{
parent::__construct();

$array = JRequest::getVar('cid', 0, '', 'array');
$this->setId((int)$array[0]);
}

/**
* Method to set the hello identifier
*
* @access public
* @param int Hello identifier
* @return void
*/
function setId($id)
{
// Set id and wipe data
$this->_id = $id;
$this->_data = null;
}

/**
* Method to get a hello
* @return object with data
*/
function &getData()
{


// Load the data
if (empty( $this->_data )) {
$query = ' SELECT * FROM #__contactus '.
' WHERE id = '.$this->_id;
$this->_db->setQuery( $query );
$this->_data = $this->_db->loadObject();
}

if (!$this->_data) {
$this->_data = new stdClass();
$this->_data->id = 0;
$this->_data->your_name = null;
$this->_data->your_email = null;
$this->_data->your_tp = null;


}

return $this->_data;
}

/**
* Method to store a record
*
* @access public
* @return boolean True on success
*/
function store()
{



$row =& $this->getTable();

$data = JRequest::get( 'post' );


// Bind the form fields to the hello table
if (!$row->bind($data)) {
$this->setError($this->_db->getErrorMsg());
return false;
}

// Make sure the hello record is valid
if (!$row->check()) {
$this->setError($this->_db->getErrorMsg());
return false;
}

// Store the web link table to the database
if (!$row->store()) {
$this->setError( $row->getErrorMsg() );
return false;
}

return true;
}

/**
* Method to delete record(s)
*
* @access public
* @return boolean True on success
*/
function delete()
{
$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

$row =& $this->getTable();

if (count( $cids )) {
foreach($cids as $cid) {
if (!$row->delete( $cid )) {
$this->setError( $row->getErrorMsg() );
return false;
}
}
}
return true;
}

}





5.com_lanka1->tables->lanka1.php




<?php
/**
* Google Map default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/


// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


class TableLanka1 extends JTable
{
/**
* Primary Key
*
* @var int
*/
var $id = null;
var $your_name = null;
var $your_email = null;
var $your_tp = null;


/**
* Constructor
*
* @param object Database connector object
*/
function TableLanka1(& $db) {
parent::__construct('#__contactus', 'id', $db);
}
}

3 comments:

  1. Hello, i cant download the file com_lanka1.zip.
    File not found. Can you fix it please.

    greets from germany
    André

    copy2k@arcor.de

    ReplyDelete
  2. thanks for ur information

    will update ASAP

    ReplyDelete
  3. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial

    Thanks a lot..!

    ReplyDelete