Skip to content

Commit ad69efa

Browse files
committed
Merge pull request #17 from iCAPLyon1/delete-fix
Delete fix
2 parents 617e9e7 + 1b1ee12 commit ad69efa

File tree

5 files changed

+94
-32
lines changed

5 files changed

+94
-32
lines changed

Controller/LessonController.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -344,37 +344,40 @@ public function deleteChapterAction($lesson, $chapterId)
344344
$form = $this->createForm(new DeleteChapterType(), $chapter);
345345
$form->handleRequest($this->getRequest());
346346

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

354-
$em = $this->getDoctrine()->getManager();
355-
if ($deleteChildren) {
356-
$em->remove($chapter);
357-
$em->flush();
358-
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted',array(), 'icap_lesson'));
359-
}
360-
else
361-
{
362-
$repo = $em->getRepository('IcapLessonBundle:Chapter');
363-
$repo->removeFromTree($chapter);
364-
//$em->clear();
365-
$em->flush();
366-
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted but no subchapter',array(), 'icap_lesson'));
367-
}
368-
$this->dispatchChapterDeleteEvent($lesson, $chaptername);
369-
return $this->redirect($this->generateUrl('icap_lesson', array('resourceId' => $lesson->getId())));
370-
} else {
371-
$this->get('session')->getFlashBag()->add('error',$translator->trans('Your chapter has not been deleted',array(), 'icap_lesson'));
352+
if($form->has('deletechildren')){
353+
$deleteChildren = $form->get('deletechildren')->getData();
354+
}
355+
356+
$em = $this->getDoctrine()->getManager();
357+
if ($deleteChildren) {
358+
$em->remove($chapter);
359+
$em->flush();
360+
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted',array(), 'icap_lesson'));
372361
}
362+
else
363+
{
364+
$repo = $em->getRepository('IcapLessonBundle:Chapter');
365+
$repo->removeFromTree($chapter);
366+
//$em->clear();
367+
$em->flush();
368+
$this->get('session')->getFlashBag()->add('success',$translator->trans('Your chapter has been deleted but no subchapter',array(), 'icap_lesson'));
369+
}
370+
$this->dispatchChapterDeleteEvent($lesson, $chaptername);
371+
372+
return $this->redirect($this->generateUrl('icap_lesson', array('resourceId' => $lesson->getId())));
373+
374+
373375
return array(
374376
'lesson' => $lesson,
375377
'chapter' => $chapter,
376378
'form' => $form->createView(),
377-
'workspace' => $lesson->getResourceNode()->getWorkspace()
379+
'workspace' => $lesson->getResourceNode()->getWorkspace(),
380+
'_resource' => $lesson
378381
);
379382
}
380383

Entity/Chapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Chapter
7373
/**
7474
* @Gedmo\TreeParent
7575
* @ORM\ManyToOne(targetEntity="Icap\LessonBundle\Entity\Chapter")
76-
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
76+
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
7777
*/
7878
private $parent;
7979

Entity/Lesson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Lesson extends AbstractResource
2121
{
2222
/**
2323
* @ORM\OneToOne(targetEntity="Icap\LessonBundle\Entity\Chapter", cascade={"all"})
24-
* @ORM\JoinColumn(name="root_id", referencedColumnName="id")
24+
* @ORM\JoinColumn(name="root_id", referencedColumnName="id", onDelete="CASCADE")
2525
*/
2626
private $root;
2727

Listener/LessonListener.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,10 @@ public function onCopy(CopyResourceEvent $event){
9292

9393
public function onDelete(DeleteResourceEvent $event)
9494
{
95-
$em = $this->container->get('doctrine.orm.entity_manager');
95+
$om = $this->container->get('claroline.persistence.object_manager');
9696
$lesson = $event->getResource();
97-
$lesson->setRoot(null);
98-
$em->flush();
99-
$em->remove($lesson);
100-
$em->flush();
97+
$om->remove($lesson);
98+
$om->flush();
10199
$event->stopPropagation();
102100
}
103101

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Icap\LessonBundle\Migrations\pdo_mysql;
4+
5+
use Doctrine\DBAL\Migrations\AbstractMigration;
6+
use Doctrine\DBAL\Schema\Schema;
7+
8+
/**
9+
* Auto-generated migration based on mapping information: modify it with caution
10+
*
11+
* Generation date: 2016/01/13 12:20:56
12+
*/
13+
class Version20160113122054 extends AbstractMigration
14+
{
15+
public function up(Schema $schema)
16+
{
17+
$this->addSql("
18+
ALTER TABLE icap__lesson_chapter
19+
DROP FOREIGN KEY FK_3D7E3C8C727ACA70
20+
");
21+
$this->addSql("
22+
ALTER TABLE icap__lesson_chapter
23+
ADD CONSTRAINT FK_3D7E3C8C727ACA70 FOREIGN KEY (parent_id)
24+
REFERENCES icap__lesson_chapter (id)
25+
ON DELETE SET NULL
26+
");
27+
$this->addSql("
28+
ALTER TABLE icap__lesson
29+
DROP FOREIGN KEY FK_D9B3613079066886
30+
");
31+
$this->addSql("
32+
ALTER TABLE icap__lesson
33+
ADD CONSTRAINT FK_D9B3613079066886 FOREIGN KEY (root_id)
34+
REFERENCES icap__lesson_chapter (id)
35+
ON DELETE CASCADE
36+
");
37+
}
38+
39+
public function down(Schema $schema)
40+
{
41+
$this->addSql("
42+
ALTER TABLE icap__lesson
43+
DROP FOREIGN KEY FK_D9B3613079066886
44+
");
45+
$this->addSql("
46+
ALTER TABLE icap__lesson
47+
ADD CONSTRAINT FK_D9B3613079066886 FOREIGN KEY (root_id)
48+
REFERENCES icap__lesson_chapter (id)
49+
");
50+
$this->addSql("
51+
ALTER TABLE icap__lesson_chapter
52+
DROP FOREIGN KEY FK_3D7E3C8C727ACA70
53+
");
54+
$this->addSql("
55+
ALTER TABLE icap__lesson_chapter
56+
ADD CONSTRAINT FK_3D7E3C8C727ACA70 FOREIGN KEY (parent_id)
57+
REFERENCES icap__lesson_chapter (id)
58+
ON DELETE CASCADE
59+
");
60+
}
61+
}

0 commit comments

Comments
 (0)