OOP Zend Framework vraag
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 :)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?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));
}
}
}
}
?>
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));
}
}
}
}
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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);
}
}
?>
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);
}
}
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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));
}
}
?>
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
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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>
$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
Gewijzigd op 04/09/2011 15:11:27 door Robert dat ben ik
Er zijn nog geen reacties op dit bericht.