Browse Source

Fix web/editor.css path when exporting/importing documents see BT#10885

jmontoyaa 6 years ago
parent
commit
06eaf17619

+ 2 - 0
src/Chamilo/CourseBundle/Component/CourseCopy/Course.php

@@ -59,6 +59,8 @@ class Course
 
     /**
      * Add a resource from a given type to this course.
+     *
+     * @param Resource $resource
      */
     public function add_resource(&$resource)
     {

+ 17 - 1
src/Chamilo/CourseBundle/Component/CourseCopy/CourseArchiver.php

@@ -4,6 +4,7 @@
 namespace Chamilo\CourseBundle\Component\CourseCopy;
 
 use Chamilo\CourseBundle\Component\CourseCopy\Resources\Asset;
+use Chamilo\CourseBundle\Component\CourseCopy\Resources\Document;
 use Symfony\Component\Filesystem\Filesystem;
 
 /**
@@ -108,12 +109,28 @@ class CourseArchiver
 
         // Copy all documents to the temp-dir
         if (isset($course->resources[RESOURCE_DOCUMENT]) && is_array($course->resources[RESOURCE_DOCUMENT])) {
+            $webEditorCss = api_get_path(WEB_CSS_PATH).'editor.css';
+            /** @var Document $document */
             foreach ($course->resources[RESOURCE_DOCUMENT] as $document) {
                 if ($document->file_type == DOCUMENT) {
                     $doc_dir = $backup_dir.$document->path;
                     @mkdir(dirname($doc_dir), $perm_dirs, true);
                     if (file_exists($course->path.$document->path)) {
                         copy($course->path.$document->path, $doc_dir);
+                        // Check if is html or htm
+                        $extension = pathinfo(basename($document->path), PATHINFO_EXTENSION);
+                        switch ($extension) {
+                            case 'html':
+                            case 'htm':
+                                $contents = file_get_contents($doc_dir);
+                                $contents = str_replace(
+                                    $webEditorCss,
+                                    '{{css_editor}}',
+                                    $contents
+                                );
+                                file_put_contents($doc_dir, $contents);
+                                break;
+                        }
                     }
                 } else {
                     @mkdir($backup_dir.$document->path, $perm_dirs, true);
@@ -171,7 +188,6 @@ class CourseArchiver
                     copyDirTo($course->path.$asset->path, $doc_dir, false);
                     continue;
                 }
-
                 copy($course->path.$asset->path, $doc_dir);
             }
         }

+ 49 - 21
src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php

@@ -288,6 +288,7 @@ class CourseRestorer
             return;
         }
 
+        $webEditorCss = api_get_path(WEB_CSS_PATH).'editor.css';
         $table = Database::get_course_table(TABLE_DOCUMENT);
         $resources = $this->course->resources;
         $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
@@ -450,6 +451,7 @@ class CourseRestorer
                             $origin_path = $this->course->backup_path.'/'.$document->path;
                             if (file_exists($origin_path)) {
                                 copy($origin_path, $path.$document->path);
+                                $this->fixEditorHtmlContent($path.$document->path, $webEditorCss);
                                 $sql = "SELECT id FROM $table
                                         WHERE
                                             c_id = ".$this->destination_course_id." AND
@@ -684,6 +686,7 @@ class CourseRestorer
                                                 $this->course->info['path']
                                             );
                                             file_put_contents($dest_document_path, $content);
+                                            $this->fixEditorHtmlContent($dest_document_path, $webEditorCss);
                                         }
                                     }
 
@@ -702,31 +705,31 @@ class CourseRestorer
                                     if ($document_id) {
                                         $sql = "UPDATE $table SET id = iid WHERE iid = $document_id";
                                         Database::query($sql);
-                                    }
 
-                                    $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
+                                        $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
 
-                                    $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : '';
-                                    $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id();
-                                    $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0;
-                                    $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null;
+                                        $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : '';
+                                        $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id();
+                                        $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0;
+                                        $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null;
 
-                                    $insertUserId = $this->checkUserId($insertUserId);
-                                    $toUserId = $this->checkUserId($toUserId, true);
-                                    $groupInfo = $this->checkGroupId($toGroupId);
+                                        $insertUserId = $this->checkUserId($insertUserId);
+                                        $toUserId = $this->checkUserId($toUserId, true);
+                                        $groupInfo = $this->checkGroupId($toGroupId);
 
-                                    api_item_property_update(
-                                        $course_info,
-                                        TOOL_DOCUMENT,
-                                        $document_id,
-                                        'DocumentAdded',
-                                        $insertUserId,
-                                        $groupInfo,
-                                        $toUserId,
-                                        null,
-                                        null,
-                                        $my_session_id
-                                    );
+                                        api_item_property_update(
+                                            $course_info,
+                                            TOOL_DOCUMENT,
+                                            $document_id,
+                                            'DocumentAdded',
+                                            $insertUserId,
+                                            $groupInfo,
+                                            $toUserId,
+                                            null,
+                                            null,
+                                            $my_session_id
+                                        );
+                                    }
                                 } else {
                                     if (file_exists($path.$document->path)) {
                                         copy($path.$document->path, $path.$new_file_name);
@@ -747,6 +750,7 @@ class CourseRestorer
                                                 $this->course->info['path']
                                             );
                                             file_put_contents($path.$new_file_name, $content);
+                                            $this->fixEditorHtmlContent($path.$new_file_name, $webEditorCss);
                                         }
                                     }
 
@@ -813,6 +817,7 @@ class CourseRestorer
                                             $this->course->info['path']
                                         );
                                         file_put_contents($path.$new_file_name, $content);
+                                        $this->fixEditorHtmlContent($path.$new_file_name, $webEditorCss);
                                     }
                                 }
 
@@ -887,6 +892,7 @@ class CourseRestorer
                                     $this->course->info['path']
                                 );
                                 file_put_contents($path.$document->path, $content);
+                                $this->fixEditorHtmlContent($path.$document->path, $webEditorCss);
                             }
                         }
 
@@ -3568,4 +3574,26 @@ class CourseRestorer
 
         return $userId;
     }
+
+    /**
+     * @param string $documentPath
+     * @param string $webEditorCss
+     */
+    public function fixEditorHtmlContent($documentPath, $webEditorCss = '')
+    {
+        $extension = pathinfo(basename($documentPath), PATHINFO_EXTENSION);
+
+        switch ($extension) {
+            case 'html':
+            case 'htm':
+                $contents = file_get_contents($documentPath);
+                $contents = str_replace(
+                    '{{css_editor}}',
+                    $webEditorCss,
+                    $contents
+                );
+                file_put_contents($documentPath, $contents);
+                break;
+        }
+    }
 }