Skip to content

Commit

Permalink
Merge pull request #17 from iCAPLyon1/delete-fix
Browse files Browse the repository at this point in the history
Delete fix
  • Loading branch information
ptsavdar committed Jan 13, 2016
2 parents 617e9e7 + 1b1ee12 commit ad69efa
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 32 deletions.
53 changes: 28 additions & 25 deletions Controller/LessonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,37 +344,40 @@ public function deleteChapterAction($lesson, $chapterId)
$form = $this->createForm(new DeleteChapterType(), $chapter);
$form->handleRequest($this->getRequest());

if($form->isValid()){
$chaptername = $chapter->getTitle();
$deleteChildren = false;
if($form->has('deletechildren')){
$deleteChildren = $form->get('deletechildren')->getData();
}
//I wish I could do the form validation, but sometimes the form is always false with not errors
//@todo add form validation
$chaptername = $chapter->getTitle();
$deleteChildren = false;

$em = $this->getDoctrine()->getManager();
if ($deleteChildren) {
$em->remove($chapter);
$em->flush();
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted',array(), 'icap_lesson'));
}
else
{
$repo = $em->getRepository('IcapLessonBundle:Chapter');
$repo->removeFromTree($chapter);
//$em->clear();
$em->flush();
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted but no subchapter',array(), 'icap_lesson'));
}
$this->dispatchChapterDeleteEvent($lesson, $chaptername);
return $this->redirect($this->generateUrl('icap_lesson', array('resourceId' => $lesson->getId())));
} else {
$this->get('session')->getFlashBag()->add('error',$translator->trans('Your chapter has not been deleted',array(), 'icap_lesson'));
if($form->has('deletechildren')){
$deleteChildren = $form->get('deletechildren')->getData();
}

$em = $this->getDoctrine()->getManager();
if ($deleteChildren) {
$em->remove($chapter);
$em->flush();
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted',array(), 'icap_lesson'));
}
else
{
$repo = $em->getRepository('IcapLessonBundle:Chapter');
$repo->removeFromTree($chapter);
//$em->clear();
$em->flush();
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted but no subchapter',array(), 'icap_lesson'));
}
$this->dispatchChapterDeleteEvent($lesson, $chaptername);

return $this->redirect($this->generateUrl('icap_lesson', array('resourceId' => $lesson->getId())));


return array(
'lesson' => $lesson,
'chapter' => $chapter,
'form' => $form->createView(),
'workspace' => $lesson->getResourceNode()->getWorkspace()
'workspace' => $lesson->getResourceNode()->getWorkspace(),
'_resource' => $lesson
);
}

Expand Down
2 changes: 1 addition & 1 deletion Entity/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Chapter
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Icap\LessonBundle\Entity\Chapter")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $parent;

Expand Down
2 changes: 1 addition & 1 deletion Entity/Lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Lesson extends AbstractResource
{
/**
* @ORM\OneToOne(targetEntity="Icap\LessonBundle\Entity\Chapter", cascade={"all"})
* @ORM\JoinColumn(name="root_id", referencedColumnName="id")
* @ORM\JoinColumn(name="root_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $root;

Expand Down
8 changes: 3 additions & 5 deletions Listener/LessonListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ public function onCopy(CopyResourceEvent $event){

public function onDelete(DeleteResourceEvent $event)
{
$em = $this->container->get('doctrine.orm.entity_manager');
$om = $this->container->get('claroline.persistence.object_manager');
$lesson = $event->getResource();
$lesson->setRoot(null);
$em->flush();
$em->remove($lesson);
$em->flush();
$om->remove($lesson);
$om->flush();
$event->stopPropagation();
}

Expand Down
61 changes: 61 additions & 0 deletions Migrations/pdo_mysql/Version20160113122054.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Icap\LessonBundle\Migrations\pdo_mysql;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated migration based on mapping information: modify it with caution
*
* Generation date: 2016/01/13 12:20:56
*/
class Version20160113122054 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql("
ALTER TABLE icap__lesson_chapter
DROP FOREIGN KEY FK_3D7E3C8C727ACA70
");
$this->addSql("
ALTER TABLE icap__lesson_chapter
ADD CONSTRAINT FK_3D7E3C8C727ACA70 FOREIGN KEY (parent_id)
REFERENCES icap__lesson_chapter (id)
ON DELETE SET NULL
");
$this->addSql("
ALTER TABLE icap__lesson
DROP FOREIGN KEY FK_D9B3613079066886
");
$this->addSql("
ALTER TABLE icap__lesson
ADD CONSTRAINT FK_D9B3613079066886 FOREIGN KEY (root_id)
REFERENCES icap__lesson_chapter (id)
ON DELETE CASCADE
");
}

public function down(Schema $schema)
{
$this->addSql("
ALTER TABLE icap__lesson
DROP FOREIGN KEY FK_D9B3613079066886
");
$this->addSql("
ALTER TABLE icap__lesson
ADD CONSTRAINT FK_D9B3613079066886 FOREIGN KEY (root_id)
REFERENCES icap__lesson_chapter (id)
");
$this->addSql("
ALTER TABLE icap__lesson_chapter
DROP FOREIGN KEY FK_3D7E3C8C727ACA70
");
$this->addSql("
ALTER TABLE icap__lesson_chapter
ADD CONSTRAINT FK_3D7E3C8C727ACA70 FOREIGN KEY (parent_id)
REFERENCES icap__lesson_chapter (id)
ON DELETE CASCADE
");
}
}

0 comments on commit ad69efa

Please sign in to comment.