Fix #7591 - scanning templates for multiple projects using Content manager
[Pman.Cms] / UpdateBjsTemplates.php
index 6e0176c..3a8cd4e 100644 (file)
@@ -2,12 +2,16 @@
 
 /**
  *
+ * simlar to Pman/Admin/UpdateBjsTemplates...
+ *
+ * scan BJS files
+ * scans JS (for  ._("...") )
+ * scan PHP for 
  *
  */
 
-require_once 'Pman.php';
-class Pman_Cms_UpdateBjsTemplates extends Pman
+require_once 'Pman/Admin/UpdateBjsTemplates.php';
+class Pman_Cms_UpdateBjsTemplates extends Pman_Admin_UpdateBjsTemplates
 {
     
     static $cli_desc = "Update BJS Templates";
@@ -39,17 +43,17 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
     
     function updateData()
     {   
-        $this->scanTemplates();
+        $this->scanFiles();
         
         $this->scanTables();
-        
         $this->syncLanguage();
         
+        
         $this->jok('OK');
         
     }
     
-    function scanTemplates()
+    function scanFiles()
     {
         $ff = HTML_FlexyFramework::get();
         
@@ -57,10 +61,32 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             $this->jerr("config[Pman_Cms] is not set");
         }
         
-        $base = $ff->Pman_Cms['project_name'];
+       
+        foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
+            $this->scanFilesBase($base);
+        }
+    }
+    
+    /*
+     * this does all the scan work.
+     * it's pretty simple (does not do subdirectories...)
+     *
+     * only supports BJS / PHP / JS currently.
+     * 
+     */
+    
+    
+    function scanFilesBase($base)
+    {
+        
+        $ff = HTML_FlexyFramework::get();
+
         $dir = $ff->Pman_Cms['site_dir'][$base];
+        //var_dump($dir);
         $dh = opendir($dir);
         
+        $tp = DB_DAtaObject::Factory('cms_template');
+        
         $ret = array();
         
         if(!$dh){
@@ -70,7 +96,41 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
          
         while (($fn = readdir($dh)) !== false) {
             
-            if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
+            if(empty($fn) || $fn[0] == '.') {
+                continue;
+            }
+            $fp = $dir . '/'. $fn;
+            //var_dump($fp);
+            if (is_dir($fp)) {
+                continue;
+            }
+            if (is_link($fp)) {
+                continue;
+            }
+            if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
+                $temp = $tp->syncJsWords(array(
+                    'base' => $base, 
+                    'template_dir' =>  $dir,
+                    'template' => $fn
+                ));
+                if ($temp) {
+                    $ids[] = $temp->id;
+                }
+                continue;
+            }
+            if (preg_match('/\.php$/', $fn)) {
+                $temp = $tp->syncPhpGetText(array(
+                    'base' =>$base, 
+                    'template_dir' =>  $dir,
+                    'template' => $fn
+                ));
+                if ($temp) {
+                    $ids[] = $temp->id;
+                }
+                continue;
+            }
+            
+            if (!preg_match('/\.bjs$/', $fn)){
                 continue;
             }
             // var_dump($fn);exit;
@@ -78,6 +138,9 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
                 echo "Processing {$fn} \n";
             }
             
+            
+            
+            
             $template = DB_DataObject::factory('cms_template');
             $template->setFrom(array(
                 'template' => $fn,
@@ -90,10 +153,18 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             if($template->find(true)){
                 $o = clone ($template);
             }
+            $template->filetype = 'bjs';
             
             $template->updated = $template->sqlValue("NOW()");
             
-            (empty($o)) ? $template->insert() : $template->update($o);
+            if (empty($o)) {
+                $ids[] = $template->insert() ;
+            } else {
+                $template->update($o);
+                $ids[] = $o->id;
+            }
+            
+            
             
             $data = json_decode(file_get_contents($base . '/' . $fn), true);
             
@@ -102,61 +173,21 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             $x = DB_DataObject::Factory('cms_templatestr');
             $x->syncTemplateWords($template, false);
         }
-        // pman templates are done via core templates
-       // $this->scanPmanTemplates();
         
-    }
-    
-    function scanPmanTemplates()
-    {
-        foreach ($this->modules() as $m){
-            $view_name = "Pman.$m";
-            
-            $dh = opendir("Pman/$m");
         
-            $ret = array();
-
-            if(!$dh){
-                continue;
-            }
-            
-            while (($fn = readdir($dh)) !== false) {
-            
-                if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
-                    continue;
-                }
-
-                if($this->cli){
-                    echo "Processing {$fn} \n";
-                }
-
-                $template = DB_DataObject::factory('cms_template');
-                $template->setFrom(array(
-                    'template' => $fn,
-                    'lang' => 'en',
-                    'view_name' => $view_name
-                ));
-
-                $o = false;
-
-                if($template->find(true)){
-                    $o = clone ($template);
-                }
-
-                $template->updated = $template->sqlValue("NOW()");
-
-                (empty($o)) ? $template->insert() : $template->update($o);
-
-                $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
-
-                $template->words = empty($data['strings']) ? array() : $data['strings'];
-
-                $x = DB_DataObject::Factory('cms_templatestr');
-                $x->syncTemplateWords($template, false);
-            }
+        $del = DB_DataObject::factory('cms_template');
+        $del->whereAddIn('!id', $ids, 'int');
+        $del->view_name = $base;
+        $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
+        $delids = $del->fetchAll('id');
+        if ($delids) {
+            DB_DataObject::factory('core_template')->query(
+                'update cms_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
+            );
         }
         
     }
+     
     
     function scanTables()
     {
@@ -186,17 +217,16 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
     {
         $ff = HTML_FlexyFramework::get();
         
-        if (empty($ff->Pman_Cms)) {
-            $this->jerr("config[Pman_Cms] is not set");
+        if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
+            $this->jerr("config[Pman_Cms][languages] is not set");
         }
         
-        $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
         
         if($this->cli){
             echo "Sync the Languages template.....\n";
         }
         
-        foreach($opt_lang['l'] as $l) {
+        foreach($ff->Pman_Cms['languages'] as $l) {
             if($this->cli){
                 echo "Sync $l Language.....\n";
             }