symfony - Preparing options for updating hierarchical categories using the Doctrine extension Tree -
i'm using doctrine extension tree managing categories in hierarchical way in symfony project.
still, haven't found out how manage add/edit part in twig : can display html tree structure childrenhierarchy()
method, go further , have :
root - cat 1 (+) -- cat 1-1 (+) -- cat 1-2 (+) - cat 2 (+)
the (+) open modal displaying (category) textfield, allowing branch element on selected parent.
i haven't seen in doc, (and don't if it's possible) use method childrenhierarchy()
options allowing html, ajax call.
my entity basic :
<?php namespace cpasimusante\simupollbundle\entity; use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; use claroline\corebundle\entity\user; use gedmo\mapping\annotation gedmo; /** * simupoll categories * * @orm\entity(repositoryclass="gedmo\tree\entity\repository\nestedtreerepository") * @orm\table( * name="cpasimusante__simupoll_category", * uniqueconstraints={ * @orm\uniqueconstraint(name="category_unique_name_and_user", columns={"user_id", "name"}) * } * ) * @doctrineassert\uniqueentity({"name", "user"}) * @gedmo\tree(type="nested") */ class category { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * name of category * @var string $value * * @orm\column(name="name", type="string", length=255, nullable=false) */ private $name; /** * @gedmo\treeleft * @orm\column(name="lft", type="integer") */ private $lft; /** * @gedmo\treelevel * @orm\column(name="lvl", type="integer") */ private $lvl; /** * @gedmo\treeright * @orm\column(name="rgt", type="integer") */ private $rgt; /** * @gedmo\treeroot * @orm\column(name="root", type="integer", nullable=true) */ private $root; /** * @gedmo\treeparent * @orm\manytoone( * targetentity="cpasimusante\simupollbundle\entity\category", * inversedby="children" * ) * @orm\joincolumn(name="parent_id", referencedcolumnname="id", ondelete="cascade") */ protected $parent; /** * @orm\manytoone(targetentity="claroline\corebundle\entity\user") */ private $user; /** * @orm\onetomany( * targetentity="cpasimusante\simupollbundle\entity\category", * mappedby="parent", * ) * @orm\orderby({"lft" = "asc"}) */ protected $children; /** * propoerty used in hierarchy display, selectbox */ private $indentedname; public function __construct() { $this->children = new arraycollection(); } /** * returns resource id. * * @return integer */ public function getid() { return $this->id; } /** * sets resource id. * required resourcecontroller when creates fictionnal root * * @param integer $id */ public function setid($id) { $this->id = $id; } public function getname() { return $this->name; } public function setname($name) { $this->name = $name; } /** * returns children resource instances. * * @return \doctrine\common\arraycollection|category[] */ public function getchildren() { return $this->children; } /** * returns parent category. * * @return \cpasimusante\simupollbundle\entity\category */ public function getparent() { return $this->parent; } /** * sets parent category. * * @param \cpasimusante\simupollbundle\entity\category $parent */ public function setparent(\cpasimusante\simupollbundle\entity\category $parent = null) { $this->parent = $parent; } /** * return lvl value of resource in tree. * * @return integer */ public function getlvl() { return $this->lvl; } public function getuser() { return $this->user; } public function setuser(user $user) { $this->user = $user; } /** * allows hierachy display * @return string */ public function __tostring() { return $this->getname(); } /** * allows hierachy display * @return string */ public function getindentedname() { return str_repeat("--", $this->lvl) . $this->name; //return str_repeat($this->getparent()." > ", $this->lvl) . $this->name; } }
my controller is, moment basic too, think potentially tweak options :
public function indexaction(simupoll $simupoll) { $em = $this->getdoctrine()->getmanager(); $user = $this->container ->get('security.token_storage') ->gettoken()->getuser(); $repo = $em->getrepository('cpasimusantesimupollbundle:category'); $options = array( 'decorate' => true, 'rootopen' => '<ul>', 'rootclose' => '</ul>', 'childopen' => '<li>', 'childclose' => '</li>' ); $htmltree = $repo->childrenhierarchy( null, /* starting root nodes */ false, /* true: load children, false: direct */ $options ); return array( '_resource' => $simupoll, 'tree' => $htmltree, ); }
the template :
{% block section_content %} <div class="panel panel-default"> <div class="panel-body" id="text_content"> {{ tree |raw }} </div> </div> {% endblock %}
thank in advance if has suggestion or lead.
in fact, nodedecorator option allowed customize output :
$options = array( 'decorate' => true, 'rootopen' => '<ul>', 'rootclose' => '</ul>', 'childopen' => '<li>', 'childclose' => '</li>', 'nodedecorator' => function($node) { return $node['name']. '<a href="#" data-id="'.$node['id'].'" class="btn btn-primary category-add-btn">+</a>'; } );
and use js in twig template display modal.
$('#treecontainer').on('click', '.category-add-btn', function(){ //logic display modal , pass parameter });
Comments
Post a Comment