Skip to content

Instantly share code, notes, and snippets.

@yuklia
Created June 17, 2015 16:12
Show Gist options
  • Save yuklia/12f2b8c1aab629e56a09 to your computer and use it in GitHub Desktop.
Save yuklia/12f2b8c1aab629e56a09 to your computer and use it in GitHub Desktop.
<?php
/**
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/
/**
* @namespace
*/
namespace Application\Groups;
use Application\Bootstrap;
use Application\Exception;
use Application\Auth;
use Application\Groupleades\Entity\Grouplead;
use Application\Groups\Entity\Group;
use Application\UsersActions;
use \adLDAP as Ldap;
use Bluz\Application\Exception\NotImplementedException;
use Bluz\Proxy\Registry;
use Bluz\Proxy\Request;
/**
* Crud
*
* @package Application\Groups
*
* @method Table getTable()
*
* @author Anton Shevchuk
* @created 30.10.12 16:11
*/
//setting up view and model
class Crud extends \Bluz\Controller\Crud
{
public function __invoke()
{
$primary = $this->getPrimaryKey(); //?
$primary = Request::getParam('id');
$result = [];
$form = null;
$builder = null;
/** @var \Doctrine\ORM\EntityManager $em */
$em = Registry::get('em');
/** @var \Application\BluzForm $bluzForm */
$bluzForm = Bootstrap::getServiceContainer()->get('bluzForm');
$groupForm = new \Groups\Forms\GroupForm();
$groupForm->setEm($em);
//render set form
/** @var \Symfony\Component\Form\Form $form */
$builder = $bluzForm->getFormFactory()
->createBuilder($groupForm , null, [])->setAction('groups/crud');
// switch by method
switch ($this->method) {
case Request::METHOD_GET:
if(empty($primary)){
$builder->setMethod(Request::METHOD_POST);
$form = $builder->getForm();
$result['method'] = Request::METHOD_POST;
}else{
// 'preferred_choices' => array('baz'),
//get first groupleades //sort and delimiter
// right dep
$builder->setMethod(Request::METHOD_POST);
/** @var \Application\Groups\Entity\Group $group */
$group = $em->getRepository('Application\Groups\Entity\Group')->find($primary);
// $group->set
$builder->setData($group); //transform view data
$form = $builder->getForm();
$result['method'] = Request::METHOD_PUT;
}
break;
case Request::METHOD_POST:
$form = $builder->getForm();
$bluzForm->hydrate($form);
/** @var \Application\Users\Entity\User $user */
$group = $form->getData();
if($form->isValid()){
//success
$em->persist($group);
$em->flush();
}
break;
case Request::METHOD_PATCH:
case Request::METHOD_PUT:
$form = $builder->getForm();
break;
case Request::METHOD_DELETE:
break;
default:
throw new NotImplementedException();
}
/** @var \Application\FormRenderInterface $renderer */
$renderer = $bluzForm->getEngine();
$renderer->setContainer([
'form' => $form->createView(),
]);
return $result = [
'form'=> $renderer('crud')
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment