Browse Source

Load documents as templates - refs #7952

Angel Fernando Quiroz Campos 9 years ago
parent
commit
199e9ac101

+ 5 - 10
main/inc/lib/elfinder/templates.php

@@ -7,13 +7,8 @@ use Chamilo\CoreBundle\Component\Editor\CkEditor\CkEditor;
 
 $template = new Template();
 
-$table = Database::get_main_table(TABLE_MAIN_SYSTEM_TEMPLATE);
-$sql = "SELECT * FROM $table";
-$result = Database::query($sql);
-$templates = Database::store_result($result, 'ASSOC');
-if (!empty($templates)) {
-    $editor = new CkEditor();
-    $templates = $editor->simpleFormatTemplates($templates);
-    $template->assign('templates', $templates);
-    $template->display('default/javascript/editor/ckeditor/templates.tpl');
-}
+$editor = new CkEditor();
+$templates = $editor->simpleFormatTemplates();
+
+$template->assign('templates', $templates);
+$template->display('default/javascript/editor/ckeditor/templates.tpl');

+ 102 - 14
src/Chamilo/CoreBundle/Component/Editor/CkEditor/CkEditor.php

@@ -137,19 +137,45 @@ class CkEditor extends Editor
     }
 
     /**
-     * @param array $templates
-     * @return null|string
+     * Get the empty template
+     * @return array
      */
-    public function simpleFormatTemplates($templates)
+    private function getEmptyTemplate()
     {
-        if (empty($templates)) {
-            return null;
-        }
+        return [[
+            'title' => get_lang('EmptyTemplate'),
+            'description' => null,
+            'image' => api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/empty.gif',
+            'html' => '
+                <!DOCYTPE html>
+                <html>
+                    <head>
+                        <meta charset="' . api_get_system_encoding() . '" />
+                    </head>
+                    <body  dir="' . api_get_text_direction() . '">
+                        <p>
+                            <br/>
+                        </p>
+                    </body>
+                    </html>
+                </html>
+            '
+        ]];
+    }
+
+    /**
+     * Get the platform templates
+     * @return array
+     */
+    private function getPlatformTemplates()
+    {
+        $entityManager = \Database::getManager();
+        $systemTemplates = $entityManager->getRepository('ChamiloCoreBundle:SystemTemplate')->findAll();
 
         $search = array('{CSS}', '{IMG_DIR}', '{REL_PATH}', '{COURSE_DIR}');
         $replace = array(
             '',
-            api_get_path(REL_CODE_PATH).'img/',
+            api_get_path(REL_CODE_PATH) . 'img/',
             api_get_path(REL_PATH),
             api_get_path(REL_DEFAULT_COURSE_DOCUMENT_PATH),
             api_get_path(REL_DEFAULT_COURSE_DOCUMENT_PATH)
@@ -157,10 +183,10 @@ class CkEditor extends Editor
 
         $templateList = array();
 
-        foreach ($templates as $template) {
-            $image = $template['image'];
+        foreach ($systemTemplates as $template) {
+            $image = $template->getImage();
             $image = !empty($image) ? $image : 'empty.gif';
-            $image = api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/'.$image;
+            $image = api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/' . $image;
 
             /*$image = $this->urlGenerator->generate(
                 'get_document_template_action',
@@ -168,17 +194,79 @@ class CkEditor extends Editor
                 UrlGenerator::ABSOLUTE_URL
             );*/
 
-            $content = str_replace($search, $replace, $template['content']);
+            $templateContent = $template->getContent();
+            $content = str_replace($search, $replace, $templateContent);
 
             $templateList[] = array(
-                'title' => get_lang($template['title']),
-                'description' => get_lang($template['comment']),
+                'title' => get_lang($template->getTitle()),
+                'description' => get_lang($template->getComment()),
                 'image' => $image,
                 'html' => $content
             );
         }
 
+        return $templateList;
+    }
 
-        return json_encode($templateList);
+    private function getPersonalTemplates($userId = 0)
+    {
+        if (empty($userId)) {
+            $userId = api_get_user_id();
+        }
+
+        $entityManager = \Database::getManager();
+        $templatesRepo = $entityManager->getRepository('ChamiloCoreBundle:Templates');
+
+        $user = $entityManager->find('ChamiloUserBundle:User', $userId);
+        $course = $entityManager->find('ChamiloCoreBundle:Course', api_get_course_int_id());
+
+        if (!$user || !$course) {
+            return [];
+        }
+
+        $courseTemplates = $templatesRepo->getCourseTemplates($course, $user);
+
+        $templateList = [];
+
+        foreach ($courseTemplates as $templateData) {
+            $template = $templateData[0];
+            $courseDirectory = $course->getDirectory();
+
+            $templateItem = [];
+            $templateItem['title'] = $template->getTitle();
+            $templateItem['description'] = $template->getDescription();
+            $templateItem['image'] = api_get_path(WEB_APP_PATH)
+                . 'home/default_platform_document/template_thumb/noimage.gif';
+            $templateItem['html'] = file_get_contents(api_get_path(SYS_COURSE_PATH)
+                . $courseDirectory . '/document' . $templateData['path']);
+
+            if (!empty($template->getImage())) {
+                $templateItem['image'] = api_get_path(WEB_COURSE_PATH)
+                    . $courseDirectory . '/upload/template_thumbnails/' . $template->getImage();
+            }
+
+            $templateList[] = $templateItem;
+        }
+
+        return $templateList;
+    }
+
+    /**
+     * Get the templates in JSON format
+     * @return string|
+     */
+    public function simpleFormatTemplates()
+    {
+        $templates = $this->getEmptyTemplate();
+
+        if (api_is_allowed_to_edit(false, true)) {
+            $platformTemplates = $this->getPlatformTemplates();
+            $templates = array_merge($templates, $platformTemplates);
+        }
+
+        $personalTemplates = $this->getPersonalTemplates();
+        $templates = array_merge($templates, $personalTemplates);
+
+        return json_encode($templates);
     }
 }

+ 51 - 0
src/Chamilo/CoreBundle/Entity/Repository/TemplatesRepository.php

@@ -0,0 +1,51 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\CoreBundle\Entity\Repository;
+
+use Doctrine\ORM\EntityRepository;
+use \Chamilo\CoreBundle\Entity\Course;
+use Chamilo\UserBundle\Entity\User;
+use \Doctrine\ORM\Query\Expr\Join;
+
+/**
+ * TemplatesRepository class
+ */
+class TemplatesRepository extends EntityRepository
+{
+    /**
+     * Get the course template for a user
+     * @param Course $course
+     * @param User $user
+     * @return ArrayCollection
+     */
+    public function getCourseTemplates(Course $course, User $user)
+    {
+        $qb = $this->createQueryBuilder('t');
+
+        $qb->select('t', 'd.path')
+            ->innerJoin(
+                'ChamiloCoreBundle:Course',
+                'c',
+                Join::WITH,
+                $qb->expr()->eq('t.courseCode', 'c.code')
+            )
+            ->innerJoin(
+                'ChamiloCourseBundle:CDocument',
+                'd',
+                Join::WITH,
+                $qb->expr()->eq('c.id', 'd.cId')
+            )
+            ->where(
+                $qb->expr()->eq('d.iid', 't.refDoc')
+            )
+            ->andWhere(
+                $qb->expr()->eq('c.id', $course->getId())
+            )
+            ->andWhere(
+                $qb->expr()->eq('t.userId', $user->getId())
+            );
+
+        return $qb->getQuery()->getResult();
+    }
+}

+ 1 - 1
src/Chamilo/CoreBundle/Entity/Templates.php

@@ -8,7 +8,7 @@ use Doctrine\ORM\Mapping as ORM;
  * Templates
  *
  * @ORM\Table(name="templates")
- * @ORM\Entity
+ * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Entity\Repository\TemplatesRepository")
  */
 class Templates
 {