mario60

TYPO3 FLOW Toy example

Oct 31st, 2012
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.88 KB | None | 0 0
  1. model FATHER
  2. ...
  3.  
  4. /**
  5.  * A Father
  6.  *
  7.  * @FLOW3\Entity
  8.  */
  9. class Father {
  10.  
  11.     /**
  12.      * The name
  13.      * @var string
  14.      */
  15.     protected $name;
  16.  
  17.     /**
  18.      * The sons
  19.      * @var \Doctrine\Common\Collections\Collection<\Mario\Basic\Domain\Model\Son>
  20.      * @ORM\OneToMany(mappedBy="father",cascade={"persist"})
  21.      */
  22.     protected $sons;
  23.  
  24. [getters and setters]
  25.  
  26.         /**
  27.      * Adds a son to this father
  28.      *
  29.      * @param \Mario\Basic\Domain\Model\Son $son
  30.      * @return void
  31.      */
  32.     public function addSon(\Mario\Basic\Domain\Model\Son $son) {
  33.         $son->setFather($this);
  34.         $this->sons->add($son);
  35.     }
  36.  
  37.  
  38. model SON
  39. ...
  40.  
  41. /**
  42.  * A Son
  43.  *
  44.  * @FLOW3\Entity
  45.  */
  46. class Son {
  47.  
  48.     /**
  49.      * The father
  50.      * @var \Mario\Basic\Domain\Model\Father
  51.      * @ORM\ManyToOne(inversedBy="sons")
  52.      */
  53.     protected $father;
  54.  
  55.     /**
  56.      * The name
  57.      * @var string
  58.      */
  59.     protected $name;
  60.  
  61. [getters and setters]
  62.  
  63. ******  template and layout for FATHER *********
  64. default.html
  65.  
  66. <!DOCTYPE html>
  67. <html>
  68. <head>
  69. <script language="javascript">
  70. lastField = 0;
  71.  
  72. function addInput2() {
  73. if (lastField <= 9) {
  74. var node = document.getElementById('sonInput'+lastField);
  75. var newNodeLI = document.createElement('li');
  76. newNodeLI.innerHTML = 'add a new son: ';
  77. var newNode = document.createElement('input');
  78. lastField += 1;
  79. newNode.setAttribute("id", "sonInput"+lastField);
  80. newNode.setAttribute("type", "text");
  81. newNode.setAttribute("value", "name of the new son "+lastField);
  82. newNode.setAttribute("name", "newFather[0][sons]["+lastField+"][name]");
  83. newNodeLI.appendChild(newNode);
  84. node.parentNode.insertBefore(newNodeLI, node.nextSibling);
  85. } else {
  86. var node = document.getElementById('sonInput'+lastField);
  87. var newNodeLI = document.createElement('li');
  88. newNodeLI.innerHTML = 'Only 10 input fields allowed.';
  89. node.parentNode.insertBefore(newNodeLI, node.nextSibling);
  90. }
  91. } </script>
  92. <meta charset="utf-8">
  93. <title><f:render section="Title" /></title>
  94. <f:base />
  95. </head>
  96.     <body>
  97.         <f:flashMessages class="flashmessages" />
  98.  
  99.         <h1><f:render section="Title" /></h1>
  100.         <f:render section="Content" />
  101.     </body>
  102. </html>
  103.  
  104. New.html (input fields for a father  and as many sons as a client wishes. See JS above)
  105.  
  106. <f:layout name="Default" />
  107. <f:section name="Title">New father</f:section>
  108. <f:section name="Content">
  109.     <p>Just fill out the following form to create a new father:</p>
  110.     <f:form action="create" name="newFather">
  111.                         <label for="name">Name</label>
  112.         <f:form.textfield property="0.name"  />
  113.                 <ul><li>
  114.                     <label for="nameson">Name of the son</label>
  115.                     <f:form.textfield property="0.sons.0.name" id="sonInput0" />
  116.                     <input type="button" onclick="addInput2()" name="add" value="Add input field" />
  117.                 </li>
  118.     </f:form>
  119. </f:section>
  120.  
  121. Echo.html
  122. .... snip .... 
  123.  
  124. Edit.html
  125. <f:layout name="Default" />
  126. <f:section name="Title">Edit father "{father.name}"</f:section>
  127. <f:section name="Content">
  128. <f:for each="{objArr}" as="son"> {son.flow3_persistence_identifier} &nbsp; {son.name} &nbsp; {son.father.name} &nbsp; <br> </f:for>
  129. <hr>
  130. <f:form action="update" object="{father}" name="father" >
  131.         <ol><li>
  132.         <label for="name">Name</label>
  133.         <f:form.textfield property="name" id="name" value="{father.name}"/>
  134.             </li>
  135. <f:if condition="{father.sons}">
  136.         <f:then>
  137. <f:for each="{objArr}" as="son" iteration="i"> {son.flow3_persistence_identifier}
  138. <li><label for="nameson">Son: {son.name}   </label>
  139. <f:form.textfield property="sons.{i.index}.__identity" value="{son.flow3_persistence_identifier}" type="hidden"/>
  140. <f:form.textfield property="sons.{i.index}.name" value="{son.name}" /></li>
  141.  </f:for></f:then>
  142. <f:else><td><p>{father.name} is without sons.</p></td></f:else>
  143. </f:if></li>
  144. <li><f:form.submit value="Update"/></li>
  145. </ol></f:form></f:section>
  146.  
  147. fatherController Echo, New, Edit, Create, Update actions
  148.  
  149.  
  150. class FatherController extends ActionController {
  151.  
  152.     /**
  153.      * @FLOW3\Inject
  154.      * @var \Mario\Basic\Domain\Repository\FatherRepository
  155.      */
  156.     protected $fatherRepository;
  157.  
  158.         /**
  159.      * @FLOW3\Inject
  160.      * @var \Mario\Basic\Domain\Repository\SonRepository
  161.      */
  162.     protected $sonRepository;
  163.  
  164.         /**
  165.      * @var \Mario\Basic\Domain\Model\Father
  166.      */
  167.     protected $father;
  168.  
  169. [index, show]
  170.  
  171.         /**
  172.      * Echo content of New form
  173.      *
  174.      * @param array<\Mario\Basic\Domain\Model\Father> $newFather
  175.      * @return void
  176.      */
  177.         public function echoAction(array $newFather) {
  178.              $this->view->assign('newFather', $newFather);
  179.     }
  180.  
  181.     /**
  182.      * Shows a form for creating a new father object
  183.          * @param array<\Test\Basic\Domain\Model\Father> $newFather
  184.      *
  185.      * @return void
  186.      */
  187.     public function newAction() {
  188.     }
  189.  
  190.     /**
  191.      * Adds the given new array of father objects to the father repository
  192.      *
  193.      * @param array<\Mario\Basic\Domain\Model\Father> $newFather An array of new fathers to add
  194.      * @return void
  195.      */
  196.     public function createAction(array $newFather) {
  197.      
  198.         $propertyMapper = new \TYPO3\FLOW3\Property\PropertyMapper();
  199.                  
  200.         foreach($newFather as $fatherArr){
  201.                         $fthArray = array();
  202.                         $sonArray = array();
  203.                 foreach($fatherArr as $k => $v){
  204.         if(!($k == 'sons')){$fthArray[$k]=$v;
  205.         }elseif(($k == 'sons')){$sonArray['sons']=$v;}}
  206.  
  207. $fatherObj = $propertyMapper->convert($fthArray, '\Mario\Basic\Domain\Model\Father');
  208.         $sons = new \Doctrine\Common\Collections\ArrayCollection();
  209.         $fatherObj->setSons($sons);
  210.         $this->fatherRepository->add($fatherObj);
  211.        
  212.     foreach($sonArray as $v){
  213.         foreach($v as $sonMain){
  214.         $sonObj = $propertyMapper->convert($sonMain,'\Mario\Basic\Domain\Model\Son');
  215.         $sonObj->setFather($fatherObj);
  216.         $this->sonRepository->add($sonObj);
  217.         $fatherObj->addSon($sonObj);
  218.         $this->fatherRepository->update($fatherObj);
  219.                     }}
  220.                 }
  221.         $this->addFlashMessage('Created a new father and his sons');
  222.         $this->redirect('index');
  223.     }
  224.  
  225.     /**
  226.      * Shows a form for editing an existing father object
  227.      *
  228.      * @param \Mario\Basic\Domain\Model\Father $father The father to edit
  229.      * @return void
  230.      */
  231.     public function editAction(\Mario\Basic\Domain\Model\Father $father) {
  232.         $this->view->assign('father', $father);
  233.          $objArr=array();
  234.          foreach($father->getSons() as $son){
  235.          $son->flow3_persistence_identifier=$this->persistenceManager->getIdentifierByObject($son);
  236.          $objArr[]=$son;}
  237.          $this->view->assign('objArr',$objArr);
  238.           }
  239.  
  240.     /**
  241.      * Updates the given father object
  242.      *
  243.      * @param \Mario\Basic\Domain\Model\Father $father The father to update
  244.      * @return void
  245.      */
  246.     public function updateAction(Father $father) {
  247.         $this->fatherRepository->update($father);
  248.                 foreach($father->getSons() as $son){
  249.                     $this->sonRepository->update($son);
  250.             }
  251.                 $this->addFlashMessage('Updated the father.');
  252.         $this->redirect('index');
  253.          }
Advertisement
Add Comment
Please, Sign In to add comment