Skip to content

Commit

Permalink
Added expetiment with modals
Browse files Browse the repository at this point in the history
  • Loading branch information
abtris committed Feb 24, 2010
1 parent 93ced51 commit fb93290
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
41 changes: 35 additions & 6 deletions application/controllers/ModalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,43 @@
*/
class ModalController extends Zend_Controller_Action
{
public function indexAction()

public function testAction()
{
$this->view->form = new Form_Items();
$this->view->modalform = new Form_NewItem();
Zend_Debug::dump($this->_request->getParams());
die();

if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
Zend_Debug::dump($formData);
$value = $this->_request->getParam('id', 0);
if (!empty($value)) {
Model_Items::addItem($value);
}

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();

$items = Model_Items::getItems();
$out="";
foreach ($items as $key => $val) {
$out.="<option value='$key'>$val</option>";
}
echo $out;
}


public function indexAction()
{
$this->view->form = $form = new Form_Items();
$this->view->modalform = $modalform = new Form_NewItem();
$modalform->setAction($this->view->url(array("controller"=>"modal","action"=>"test")));

if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
// form
if ($form->isValid($formData)) {
Zend_Debug::dump($formData);
} else {
$form->populate($formData);
}
}
}
}
4 changes: 4 additions & 0 deletions application/forms/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function init()
'Label' => "Add new item"
));

$this->addElement('text', 'name', array(
'Label' => 'Name'
));

$this->addElement('submit', 'save', array(
'Label' => "Save",
'ignored' => true
Expand Down
9 changes: 4 additions & 5 deletions application/forms/NewItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class Form_NewItem extends Zend_Form
{
public function init()
{

$this->setName('additem');

$this->addElement('text', 'value', array(
'Label' => "New Item",
));

$this->addElement('submit', 'add', array(
'Label' => "Add"
));


}
}
20 changes: 16 additions & 4 deletions application/models/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
*/
class Model_Items
{

const FILENAME = '/tmp/kosik';

public static function getItems()
{
$filename = '/tmp/kosik';
if (!file_exists($filename)) {
if (!file_exists(self::FILENAME)) {
$data = "1;Item 1\n2;Item 2";
file_put_contents($filename, $data);
file_put_contents(self::FILENAME, $data);
}

$data = file_get_contents($filename);
$data = file_get_contents(self::FILENAME);
$rows = explode("\n", $data);
foreach ($rows as $row) {
$items = explode(";",$row);
$out[$items[0]] = $items[1];
}
return $out;
}

public static function addItem($value)
{
$data = file_get_contents(self::FILENAME);
$rows = explode("\n", $data);
$i = count($rows);
$i++;
$data .= "\n$i;$value";
file_put_contents(self::FILENAME, $data);
}
}
36 changes: 31 additions & 5 deletions application/views/scripts/modal/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@
.mouseup(function(){
$(this).removeClass("ui-state-active");
});
$("#accordion").accordion({
event: "mouseover",
autoHeight: false

});

});

function submitFormWithAjax(form)
{
form = $(form);
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: (form.attr('method')),
dataType: 'script'
});
return false;
}

</script>

<style type="text/css">
Expand All @@ -45,7 +54,24 @@
<?php
$content = $this->modalform;

echo $this->dialogContainer("dialog", $content, array('title' => 'Login','bigiframe' => true, 'modal' => true, 'resizable'=> false, 'autoOpen' =>false), array());
echo $this->dialogContainer("dialog", $content, array('title' => 'Add',
'bigiframe' => true,
'modal' => true,
'resizable'=> false,
'autoOpen' =>false,
'buttons' => array(
'Save'=> "function() { submitFormWithAjax($('#additem')); }",
'Cancel'=> "function() { $('#dialog').dialog('close'); }"
)),
array()
);

/*
buttons: {
'Save': function() {submitFormWithAjax($(this).find('form'));},
'Cancel': function() {$(this).dialog('close');}
},
*/

?>

Expand Down

0 comments on commit fb93290

Please sign in to comment.