fix #7737 - merge filetype additions into single function, support for powerpoint
authorAlan <alan@roojs.com>
Thu, 3 Aug 2023 04:27:19 +0000 (12:27 +0800)
committerAlan <alan@roojs.com>
Thu, 3 Aug 2023 04:27:19 +0000 (12:27 +0800)
DataObjects/Core_template.php
DataObjects/Core_templatestr.php

index 1492f52..e64c9a8 100644 (file)
@@ -349,8 +349,13 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
     
     }
     
-    
-    function syncPhpGetText($pgdata)
+    // allow reuse in cms templatstr
+    function factoryStr()
+    {
+        return DB_DataObject::factory('core_templatestr');
+    }
+
+    function syncFileWord($pgdata, $filetype)
     {
         $tmpl = DB_DataObject::Factory($this->tableName());
         $tmpl->view_name = $pgdata['base'];
@@ -358,79 +363,87 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         
         if ($tmpl->get('template',  $pgdata['template'])) {
             if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
-                if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != 'html') {
+                if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != $filetype) {
                     $oo = clone($tmpl);
                     $tmpl->is_deleted = 0;
-                    $tmpl->filetype = 'php';
+                    $tmpl->filetype = $filetype;
                     $tmpl->update($oo);
                 }
                 return $tmpl;
             }
         }
+
         $words = array();
-        
-        $ar = token_get_all(file_get_contents( $tmpl->currentTemplate  ));
-        foreach( $ar as $i=> $tok) {
-            if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
-                continue;
-            }
-            if ($i < 2) {
-                continue;
-            }
-            if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
-                continue;
-            }
-            if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
-                continue;
-            }
-            $ct = $tok[1][0];
-            $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
-            
+
+        switch($filetype) {
+            case "php":
+                $ar = token_get_all(file_get_contents( $tmpl->currentTemplate  ));
+                foreach( $ar as $i=> $tok) {
+                    if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
+                        continue;
+                    }
+                    if ($i < 2) {
+                        continue;
+                    }
+                    if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
+                        continue;
+                    }
+                    if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
+                        continue;
+                    }
+                    $ct = $tok[1][0];
+                    $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
+                    
+                }
+                break;
+            case "js":
+                $fc = file_get_contents( $tmpl->currentTemplate );
+        
+                preg_match_all('/\._\("([^"]+)"\)/', $fc, $outd);
+                $words = $outd[1];
+                 
+                preg_match_all('/\._\(\'([^\']+)\'\)/', $fc, $outs);
+                
+                // ?? seriously adding two arrays?
+                $words =  array_diff(array_merge($words, $outs[1]), array_intersect($words, $outs[1]));
+                break;
+            case "xml":
+                $words = $pgdata['words'];
+                break;
         }
-        // create the template...
-        
-        
-        if (!$tmpl->id) {
-            
-            $tmpl->template = $pgdata['template'];
-            $tmpl->lang = 'en'; /// ??? hard coded??
-            $tmpl->filetype = 'php';
+
+        $words = array_unique($words);
+
+        if(empty($words)) {
+            return;
+        }
+
+        if ($tmpl->id) {
+            $oo = clone($tmpl);
             $tmpl->is_deleted = 0;
+            $tmpl->filetype = $filetype;
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->insert();
+            $tmpl->update($oo);
         } else {
-            $xx =clone($tmpl);
-            $tmpl->filetype = 'php';
             $tmpl->is_deleted = 0;
-            $tmpl->lang = 'en'; /// ??? hard coded??
+            $tmpl->filetype = $filetype;
+            $tmpl->lang = 'en';
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->update($xx);
-        }
-      
-        $words = array_unique($words);
-        
-        if (!count($words)) {
-            return;
+            $tmpl->insert();
         }
-        
-             
+
         $tmpl->words = $words;
-            
-        $x = DB_DataObject::Factory('core_templatestr');
-        $this->factoryStr()->syncTemplateWords($tmpl);     
-         
-        
+
+        $this->factoryStr()->syncTemplateWords($tmpl);
+
         return $tmpl;
-        
-        
-        
     }
-    // allow reuse in cms templatstr
-    function factoryStr()
+
+    function syncPhpGetText($pgdata)
     {
-        return DB_DataObject::factory('core_templatestr');
+        return $this->syncFileWord($pgdata, 'php'); 
     }
-    
+
     /**
      * plain JS files use ._(....) to flag 
      * it does not support quoted strings or anything really
@@ -439,58 +452,12 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
     
     function syncJsWords($pgdata)
     {
-        $tmpl = DB_DataObject::Factory($this->tableName());
-        $tmpl->view_name = $pgdata['base'];
-        $tmpl->currentTemplate = $pgdata['template_dir'] . '/'. $pgdata['template'];
-        
-        if ($tmpl->get('template',  $pgdata['template'])) {
-            if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
-                if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != 'js') {
-                    $oo = clone($tmpl);
-                    $tmpl->is_deleted = 0;
-                    $tmpl->filetype = 'js';
-                    $tmpl->update($oo);
-                }
-                return $tmpl;
-            }
-        }
-        $words = array();
-        
-        $fc = file_get_contents( $tmpl->currentTemplate );
-        
-        preg_match_all('/\._\("([^"]+)"\)/', $fc, $outd);
-        $words = $outd[1];
-         
-        preg_match_all('/\._\(\'([^\']+)\'\)/', $fc, $outs);
-        
-        // ?? seriously adding two arrays?
-        $words =  array_diff(array_merge($words, $outs[1]), array_intersect($words, $outs[1]));
-        $words = array_unique($words);
-        
-        if (empty($words)) {
-            return;
-        }
-        if ($tmpl->id) {
-            $tmpl->is_deleted = 0;
-            $tmpl->filetype = 'js';
-            $tmpl->update($tmpl);
-        } else {
-            $tmpl->is_deleted = 0;
-            $tmpl->filetype = 'js';
-            $tmpl->lang = 'en';
-            $tmpl->insert();
-        }
-        
-             
-        $tmpl->words = $words;
-            
-        $this->factoryStr()->syncTemplateWords($tmpl);    
-         
-        
-        return $tmpl;
-        
-        
-        
+        return $this->syncFileWord($pgdata, 'js');   
+    }
+
+    function syncPowerpointXMLText($pgdata) 
+    {
+        return $this->syncFileWord($pgdata, 'xml');
     }
     
     /*
index e927fba..9d2e4ca 100644 (file)
@@ -101,6 +101,19 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
 
         foreach($cols as $c) {
             $x = $this->factory($this->tableName());
+            if(strpos($c, ',') !== false) {
+                $arr = explode(',', $c);
+                $c = $arr[0];
+                $cond = $arr[1];
+
+                $ar = explode('=', $cond);
+                $key = $ar[0];
+
+                // skip if condition not fulfilled
+                if($obj->{$ar[0]} != $ar[1]) {
+                    continue;
+                }
+            }
             $x->on_id = $obj->pid();
             $x->on_table = $tn;
             $x->on_col = $c;