Joomla! 1.5 için oluşturduğumuz basit bileşenimizi kodlamanın sonuna geldik. Bu son aşamayla birlikte elimizde oldukça basit bir MVC yapısına uygun bileşenimiz olacak.
Son yazımda bileşenimizin yönetim bölümüne veritabanındaki karşılama mesajlarını listeleyen bir sayfa oluşturmuş ve ayrıca araç çubuğundaki butonlarımızı belirlemiştik. Şimdi bu butonlara işlevsellik kazandırmak için bileşenimizin yönetim bölümü dosyaları üzerinde değişiklikler yapalım. Öncelikle hazırladığımız default.php şablon dosyamızda bazı değişiklikler yapalım. default.php dosyamızda şöyle bir tablo alanı oluşturmuştuk;
Kod:
<thead>
<tr>
<th width="5">
<?php echo JText::_( 'ID' ); ?>
</th>
<th>
<?php echo JText::_( 'Karsilama' ); ?>
</th>
</tr>
</thead>
Bu alanı şu hale getiriyoruz;
Kod:
<thead>
<tr>
<th width="5">
<?php echo JText::_( 'ID' ); ?>
</th>
<th width="20">
<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this-/>items ); ?>);" />
</th>
<th>
<?php echo JText::_( 'Karsilama' ); ?>
</th>
</tr>
</thead>
Burada eklediğimiz kodlar;
Kod:
<th width="20">
<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this-/>items ); ?>);" />
</th>
Daha sonra aynı default.php içerisinde;
Kod:
$row =& $this->items[$i];
olan kısmı şöyle değiştiriyoruz;
Kod:
$row = &$this->items[$i];
$checked = JHTML::_('grid.id', $i, $row->id );
$link = JRoute::_( 'index.php?option=com_merhaba&controller=merhaba&task=edit&cid[]='. $row->id );
ve hemen altında;
Kod:
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $row->karsilama; ?>
</td>
</tr>
olan kodları şöyle değiştiriyoruz;
Kod:
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $checked; ?>
</td>
<td>
<a href="<?php echo $link; ?>"><?php echo $row->karsilama; ?></a>
</td>
</tr>
Böylece merhabalar listesinden bir merhaba alanını veya tümünü seçmek için bir checkbox oluşturmuş olduk. default.php dosyasının son hali şu şekilde oldu;
default.php:
Kod:
<?php defined('_JEXEC') or die('Restricted access'); ?>
<form action="index.php" method="post" name="adminForm">
<div id="editcell">
<table class="adminlist">
<thead>
<tr>
<th width="5">
<?php echo JText::_( 'ID' ); ?>
</th>
<th width="20">
<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this-/>items ); ?>);" />
</th>
<th>
<?php echo JText::_( 'Karsilama' ); ?>
</th>
</tr>
</thead>
<?php
$k = 0;
for ($i=0, $n=count( $this->items ); $i < $n; $i++)
{
$row = &$this->items[$i];
$checked = JHTML::_('grid.id', $i, $row->id );
$link = JRoute::_( 'index.php?option=com_merhaba&controller=merhaba&task=edit&cid[]='. $row->id );
?>
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $checked; ?>
</td>
<td>
<a href="<?php echo $link; ?>"><?php echo $row->karsilama; ?></a>
</td>
</tr>
<?php
$k = 1 - $k;
}
?>
</table>
</div>
<input type="hidden" name="option" value="com_merhaba" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="merhaba" />
</form>
Bu listeden bir merhaba seçeneğini seçtiğimizde yapılacak işlemler için öncelikle controller oluşturmamız gerekiyor. Bunun için ise controllers adında bir klasör oluşturup içerisine bir merhaba controller hazırlamamız gerekiyor.
admin/controllers/merhaba.php:
Kod:
<?php
// Direkt erişimi engelleyelim
defined( '_JEXEC' ) or die( 'Restricted access' );
/**
* Merhaba Merhaba Controller
*/
class MerhabalarControllerMerhaba extends MerhabalarController
{
/**
* oluşturucu (metodlara ek task özellikleri kayıt eder)
* @return ****
*/
function __construct()
{
parent::__construct();
// Ek taskları kayıt edelim
$this->registerTask( 'add', 'edit' );
}
/**
* düzenleme formunu gösterelim
*/
function edit()
{
JRequest::setVar( 'view', 'merhaba' );
JRequest::setVar( 'layout', 'form' );
JRequest::setVar('hidemainmenu', 1);
parent::display();
}
/**
* veriyi kaydedelim (ve bileşen ana sayfamıza geri dönelim)
*/
function save()
{
$model = $this->getModel('merhaba');
if ($model->store($post)) {
$msg = JText::_( 'Karşılama Kaydedildi!' );
} else {
$msg = JText::_( 'Karşılama Kaydedilirken Hata Oluştu' );
}
// Geriye dönüş sağlayalım
$link = 'index.php?option=com_merhaba';
$this->setRedirect($link, $msg);
}
/**
* verileri silme
*/
function remove()
{
$model = $this->getModel('merhaba');
if(!$model->delete()) {
$msg = JText::_( 'Hata: Bir veya Daha fazla karşılama silinemedi' );
} else {
$msg = JText::_( 'Karşılama(lar) silindi' );
}
$this->setRedirect( 'index.php?option=com_merhaba', $msg );
}
/**
* veri düzenlemeyi iptal edelim
*/
function cancel()
{
$msg = JText::_( 'İşlem İptal Edildi' );
$this->setRedirect( 'index.php?option=com_merhaba', $msg );
}
}