Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[admin] Finishing the placement of new menu items in the admin with f…

…unctional test.

Basically, a new menu item has a "parent_id" field you can use to sort, but existing items don't (you'd use the reordering for this).

This can be improved upon later, for now it was simpler to make sure that nodes weren't being moved across trees etc.
  • Loading branch information...
commit 89e9794ba72645e9f020495cd1b63a9ac3e8f1c7 1 parent cc9c928
@weaverryan authored
View
62 lib/form/doctrine/PluginioDoctrineMenuItemForm.class.php
@@ -35,6 +35,7 @@ public function setup()
$this->widgetSchema->setLabel('attributes', 'HTML attributes');
$this->widgetSchema->setHelp('attributes', '(e.g. title="my menu" class="homepage"');
+ $this->widgetSchema['attributes']->setAttribute('size', 72);
$this->widgetSchema->setLabel('route', 'Route/Url');
$this->widgetSchema->setHelp('route', '(e.g. @homepage or http://www.google.com');
@@ -66,26 +67,34 @@ public function setup()
}
- // add the parent_id positioning function
- $q = $this->getObject()->getTable()->getParentIdQuery();
- if (!$this->getObject()->isNew())
+ /*
+ * Add the parent_id parent node functionality if this is a new item
+ */
+ if ($this->isNew())
{
- $q->andWhere('m.id != ?', $this->object->id);
- }
+ $q = $this->getObject()->getTable()->getParentIdQuery();
+ if (!$this->getObject()->isNew())
+ {
+ $q->andWhere('m.id != ?', $this->object->id);
+ }
- $this->widgetSchema['parent_id'] = new sfWidgetFormDoctrineChoice(array(
- 'model' => 'ioDoctrineMenuItem',
- 'add_empty' => '',
- 'order_by' => array('root_id, lft', ''),
- 'query' => $q,
- 'method' => 'getIndentedName'
- ));
- $this->validatorSchema['parent_id'] = new sfValidatorDoctrineChoice(array(
- 'required' => true,
- 'model' => 'ioDoctrineMenuItem'
- ));
- $this->setDefault('parent_id', $this->object->getParentId());
- $this->widgetSchema->setLabel('parent_id', 'Child of');
+ $model = get_class($this->getObject());
+ // if new, blank means new root, else is just blank and we require the field
+ $this->widgetSchema['parent_id'] = new sfWidgetFormDoctrineChoice(array(
+ 'model' => $model,
+ 'add_empty' => 'New root menu item',
+ 'order_by' => array('root_id, lft', ''),
+ 'query' => $q,
+ 'method' => 'getIndentedName',
+ ));
+ $this->validatorSchema['parent_id'] = new sfValidatorDoctrineChoice(array(
+ 'required' => false,
+ 'model' => $model,
+ ));
+ $this->setDefault('parent_id', $this->object->getParentId());
+ $this->widgetSchema->setLabel('parent_id', 'Child of');
+ $this->widgetSchema->setHelp('parent_id', 'Choose the parent menu item for this new menu or create it as a root. After saving, you can continue to reorder the menu item.');
+ }
}
/**
@@ -98,12 +107,19 @@ protected function doSave($con = null)
$node = $this->object->getNode();
$parentId = $this->getValue('parent_id');
- if ($parentId != $this->object->getParentId() || !$node->isValidNode())
+ if ($this->isNew())
{
- //form validation ensures an existing ID for $this->parentId
- $parent = $this->object->getTable()->find($parentId);
- $method = ($node->isValidNode() ? 'move' : 'insert') . 'AsLastChildOf';
- $node->$method($parent); //calls $this->object->save internally
+ if (!$parentId)
+ {
+ // we're new and we have no parent id, so save as root
+ $this->getObject()->getTable()->getTree()->createRoot($this->getObject()); //calls $this->object->save internally
+ }
+ else
+ {
+ //form validation ensures an existing ID for $this->parentId
+ $parent = $this->object->getTable()->find($parentId);
+ $node->insertAsLastChildOf($parent); //calls $this->object->save internally
+ }
}
}
}
View
5 modules/io_doctrine_menu/config/generator.yml
@@ -28,12 +28,11 @@ generator:
form:
display:
<?php if (Doctrine_Core::getTable('ioDoctrineMenuItem')->isI18n()): ?>
- General: [name, route, attributes]
+ General: [name, _parent_id, route, attributes]
Labels: [<?php echo implode(', ', array_keys(sfConfig::get('app_doctrine_menu_i18n_cultures', array()))) ?>]
<?php else: ?>
- General: [name, label, route, attributes]
+ General: [name, _parent_id, label, route, attributes]
<?php endif; ?>
- Position: [parent_id]
Security: [requires_auth, requires_no_auth, permissions_list]
edit: ~
new: ~
View
21 modules/io_doctrine_menu/templates/_parent_id.php
@@ -0,0 +1,21 @@
+<?php if (isset($form['parent_id'])): ?>
+ <div class="sf_admin_form_row<?php $form['parent_id']->hasError() and print ' errors' ?>">
+ <?php echo $form['parent_id']->renderError() ?>
+ <div>
+ <?php echo $form['parent_id']->renderLabel() ?>
+
+ <div class="content"><?php echo $form['parent_id']->render($attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes) ?></div>
+
+ <div class="help"><?php echo $form->getWidgetSchema()->getHelp('parent_id') ?></div>
+ </div>
+ </div>
+<?php else: ?>
+ <div class="sf_admin_form_row">
+ <div>
+ <label>Position</label>
+ <div class="content">
+ <?php echo link_to('Reorder this menu item', 'io_doctrine_menu_reorder', $form->getObject()) ?>
+ </div>
+ </div>
+ </div>
+<?php endif; ?>
View
88 test/functional/menuAdminTest.php
@@ -0,0 +1,88 @@
+<?php
+// functional test for a few special things in the admin module
+require_once dirname(__FILE__).'/../bootstrap/functional.php';
+require_once sfConfig::get('sf_lib_dir').'/test/unitHelper.php';
+
+$browser = new sfTestFunctional(new sfBrowser());
+$browser->setTester('doctrine', 'sfTesterDoctrine');
+
+$arr = create_doctrine_test_tree($browser->test());
+$root = $arr['rt'];
+$rootForm = new ioDoctrineMenuItemForm($root);
+
+$browser->info('1 - Edit an existing, you cannot place it via parent_id')
+ ->get(sprintf('/test/menu/%d/edit', $root->id))
+
+ ->with('request')->begin()
+ ->isParameter('module', 'io_doctrine_menu')
+ ->isParameter('action', 'edit')
+ ->end()
+
+ ->with('response')->begin()
+ ->checkForm($rootForm)
+ ->info(' 1.1 - The root menu has no parent_id field')
+ ->checkElement('#io_doctrine_menu_item_parent_id', 0)
+ ->end()
+
+ ->click('Save', array('io_doctrine_menu_item' => array('parent_id' => $root->id)))
+
+ ->with('form')->begin()
+ ->hasErrors(1)
+ ->hasGlobalError('extra_fields')
+ ->end()
+;
+
+$browser->info('2 - Create a new menu item, set it as a child of something')
+ ->get('/test/menu/new')
+
+ ->with('request')->begin()
+ ->isParameter('module', 'io_doctrine_menu')
+ ->isParameter('action', 'new')
+ ->end()
+
+ ->with('response')->begin()
+ ->checkForm('ioDoctrineMenuItemForm')
+ ->info(' 2.1 - The root menu has a parent_id field')
+ ->checkElement('#io_doctrine_menu_item_parent_id', 1)
+ ->end()
+
+ ->click('Save', array('io_doctrine_menu_item' => array('parent_id' => $root->id, 'name' => 'new child')))
+
+ ->with('form')->begin()
+ ->hasErrors(0)
+ ->end()
+
+ ->with('doctrine')->begin()
+ ->check('ioDoctrineMenuItem', array(
+ 'name' => 'new child',
+ 'root_id' => $root->id,
+ 'lft' => 16,
+ 'rgt' => 17,
+ 'level' => 1,
+ ))
+ ->end()
+;die;
+
+$browser->info('3 - Create a new menu item, make it root')
+ ->get('/test/menu/new')
+
+ ->with('request')->begin()
+ ->isParameter('module', 'io_doctrine_menu')
+ ->isParameter('action', 'new')
+ ->end()
+
+ ->click('Save', array('io_doctrine_menu_item' => array('name' => 'new root')))
+
+ ->with('form')->begin()
+ ->hasErrors(0)
+ ->end()
+
+ ->with('doctrine')->begin()
+ ->check('ioDoctrineMenuItem', array(
+ 'name' => 'new root',
+ 'lft' => 18,
+ 'rgt' => 19,
+ 'level' => 0,
+ ))
+ ->end()
+;
Please sign in to comment.
Something went wrong with that request. Please try again.