hallo ik had een vraagje

Ik ben nu een klein weekje bezig met Zend framework

en nu vraag ik me af of ik dit zo goed doe, voor ik verder ga :)


<?php

class GuestbookController extends Zend_Controller_Action
{
    private $guestbook;
    private $form;
    

    public function init()
    {
        $this->guestbook = new Application_Model_DbTable_Guestbook_Db();
        $this->form = new Application_Form_Guestbook_Form();
    }

    public function indexAction()
    {
        $this->view->guestbook = $this->guestbook->fetchAll();
    }
    
    public function addAction()
    {
        $this->form->submit->setLabel('Add Message');
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($this->form->isValid($formData)) {              
                $username = $this->form->getValue('username');
                $email    = $this->form->getValue('email');
                $message  = $this->form->getValue('message');
                $date     = date("y-m-d H:m:s");
                $this->guestbook->addMessage($username, $email, $message, $date);
                $this->_helper->redirector('index');
            } else {
                $this->form->populate($formData);
            }
        }   
    }
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $this->guestbook->deleteMessage($id);
            }
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $this->view->guestbook = $this->guestbook->getMessage($id);
        }
    }
    
    
    public function editAction()
    {
        $this->form->submit->setLabel('Save');
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($this->form->isValid($formData)) {
                $id       = (int)$this->form->getValue('id');
                $username = $this->form->getValue('username');
                $message  = $this->form->getValue('message');
                $this->guestbook->updateMessage($id, $message);
                $this->_helper->redirector('index');
            } else {
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $this->form->populate($this->guestbook->getMessage($id));
            }
        }
    }    
}

?>




<?php

class Application_Model_DbTable_Guestbook_Db extends Zend_Db_Table_Abstract
{

    protected $_name = 'guestbook';

    public function init()
    {
        $row = $this->fetchAll();
        if (!$row) {
            throw new Exception("Could not find any guestbook messages");
        }
        return $row->toArray();
    }
    
    public function addMessage($username, $message, $date)
    {
        $data = array(
            'username' => $username,
            'message' => $message,
            'date' => $date,
        );
        $this->insert($data);
    }
    
    public function deleteMessage($id)
    {
        $this->delete('id =' . (int)$id);
    }
    
    public function getMessage($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }
    public function updateMessage($id, $message)
    {
        $data = array('message' => $message);
        $this->update($data, 'id = '. (int)$id);
    }
}
?>





<?php

class Application_Form_Guestbook_Form extends Zend_Form
{

    public function init()
    {
        $this->setName('guestbook');

        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('Username')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('regex', false, array('/^[a-zA-Z]+/'))
            ->addValidator('stringLength', false, array(6, 20))
            ->addValidator('NotEmpty');       
        
        
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('EmailAddress')
            ->addValidator('NotEmpty');
        
        $captcha = new Zend_Form_Element_Captcha('image', 
            array(
                'label' => "Captcha",
                'captcha' => 'Image',
                'captchaOptions' => 
            array(
                'captcha' => 'Image',
                'wordLen' => 4,
                'timeout' => 300,
                'imgDir' => '../public/images/captcha',
                'width' => 155,
                'height' => 45,
                'font' => 'fonts/verdana.ttf'
            ),
        ));
        
        $message = new Zend_Form_Element_Textarea('message');
        $message->setLabel('Message')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('regex', false, array('/^[a-zA-Z]+/'))
            ->addValidator('stringLength', false, array(6, 500))
            ->addValidator('NotEmpty');
        
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        
        $this->addElements(array($username, $email, $captcha, $message, $submit));
    }
}
?>




dit is de gastenboek view index.phtml

<?php         
$this->title = "My Guestbook";
$this->headTitle($this->title);
$auth = Zend_Auth::getInstance();
?>
<p><a href="<?php echo $this->url(array('controller'=>'guestbook', 
        'action'=>'add'));?>">Add new message</a></p>
<table>
<tr>
    <th class="username">Post By</th>
    <th class="message">Message</th>
    <?php
    if ($auth->hasIdentity() && $auth->getIdentity('id') == "admin") {?>
        <th class="options">Option</th>
    <?}?>
</tr>
<?php foreach($this->guestbook as $guestbook) : ?>
<tr>
    <td class="username"><?php echo $this->escape($guestbook->username);?></td>
    <td class="message"><?php echo Application_Model_Parser_Html::Pars($guestbook->message);?></td>
    <?php
    if ($auth->hasIdentity() && $auth->getIdentity('id') == "admin") {?>
    <td class="options">
        <a href="<?php echo $this->url(array('controller'=>'guestbook', 
            'action'=>'edit', 'id'=>$guestbook->id));?>">Edit</a>
        <a href="<?php echo $this->url(array('controller'=>'guestbook', 
            'action'=>'delete', 'id'=>$guestbook->id));?>">Delete</a>
    </td>
    <?}?>
</tr>
<?php endforeach; ?>
</table>



m.v.g Rob

Reageren