fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_template_element.php
1 <?php
2 /**
3  * Table Definition for core_template 
4  *
5  *
6  * The idea here is that it contains all the strings in the templates with a language '' (empty)
7  * , it then generates a matching set of strings
8  * 
9  */
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_Core_template_element  extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'core_template_element';         // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $template_id;                           // string(64)  not_null
20     public $name;
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23     
24     
25     
26     function applyFilters($q, $au, $roo)
27     {
28         if (isset($q['_core_page_id'] )) {
29             if (empty($q['_core_page_id'] )) {
30                 $this->whereAdd('1=0');
31                 return;
32             }
33             $p = DB_DataObject::factory('core_page');
34             $p->get($q['_core_page_id']);
35             $this->template_id = $p->template_id;
36             
37             
38         }
39         
40         
41     }
42     /**
43      *
44      *  the purpose of this is to find all the block[xxxx]
45      *  and make a row for then...
46      *
47      * it should return all the 
48      */
49     
50     
51     function syncTemplateElement($template)
52     {
53         $tn = $this->tableName();
54         
55         $contents = file_get_contents($template->currentTemplate);
56         $contentStrings = $template->contentStrings;
57         
58         //print_R($template->contentStrings);
59         
60         // this should also look for <flexy:use\s+content="\{block\[([A-Za-z0-9_]+)\]^+"\s" (default_content) </flexy:use>
61         // if it finds this
62         
63         // -> make sure there is a reference to element in core_template_element...
64         // -> System PAGE and elements???? with that text as the contents..
65         $core_page_strings = array();
66         
67         $matches = array();
68         if (preg_match_all('/flexy\:content=\"(\{block\[([A-Za-z0-9_-]+)\]\.([A-Za-z0-9_]+)([^"]*))"/',
69             $contents,
70             $matches
71         )) {
72             // print_R($matches);
73             // remove the full match..
74             // so that the match string matches the old format.
75             // the match[0] will be the one them matches contentStrings
76             array_shift($matches);
77             foreach($matches[0] as $i=>$k) {
78                 if (!isset($contentStrings[$k])) {
79                     continue;
80                 }
81                 //print_R($matches);
82                 if (!isset($core_page_strings[ $matches[1][$i] ])) {
83                     $core_page_strings[ $matches[1][$i] ] = array();
84                 }
85                 $core_page_strings[ $matches[1][$i] ][ $matches[2][$i] ] = trim($contentStrings[$k]);
86 //                print_r(trim($contentStrings[$k]));
87             }
88         }
89         
90         
91         
92         $old_matches = array();
93         if (preg_match_all('#\{block\[([A-Za-z0-9_-]+)\]\.#',
94                 $contents,
95                 $old_matches
96         )) {
97             
98             // pushes old matches onto new ones..
99             foreach($old_matches[0] as $i =>$v) {
100                 $matches[0][] = $v;
101                 $matches[1][] = $old_matches[1][$i];
102                 
103                 if (!isset($core_page_strings[ $matches[1][$i] ])) {
104                     $core_page_strings[ $matches[1][$i] ] = array(
105                         'title' => trim($matches[1][$i]),
106                         'body' => 'Fill in text here'
107                         
108                     );
109                     
110                 }
111 //                print_r(trim($matches[1][$i]));
112             }
113         }
114         //print_r($core_page_strings);
115         // why delete the template???
116         
117         // if (empty($matches[0]) && empty($old_matches[0])) {
118         //     $this->query("
119         //         DELETE FROM {$tn} WHERE template_id = {$template->id}
120         //     ");
121         //     return;
122         // }
123          
124          
125           
126         /// ---- USE THE SAME CODE - 
127         
128         
129         $elements = array_unique($matches[1]);
130         //print_r($elements);
131         
132         $ret = array();
133         
134         $t = DB_DataObject::Factory($tn);
135         $t->template_id = $template->id;
136         $base = clone($t);
137         $old = $t->fetchAll('name', 'id');
138         
139         foreach($elements as $el) {
140             
141             if (!isset($old[$el])) {
142                 $t = clone($base);
143                 $t->name = $el;
144                 $t->content_strings = isset($core_page_strings[$el]) ? $core_page_strings[$el] : array();
145                 
146                 $t->insert();
147                 $ret[] = clone($t);
148             } else {
149                 $t =DB_DataObject::Factory($tn);
150                 $t->get($old[$el]);
151                 $t->content_strings = isset($core_page_strings[$el]) ? $core_page_strings[$el] : array();
152                 unset($old[$el]);
153                 $ret[] = clone($t);
154                 // got element already.. ignore it..
155                 
156             }
157             // add
158             
159         }
160         
161         // delete elements, and  pages pointing to this element.. --- sounds about right..
162         
163         foreach($old as $n=>$id) {
164             $t = DB_DataObject::Factory($tn);
165             $t->get($id);
166             $t->delete();
167             // de'reference the core_pages that refered to it..
168             $core = DB_DataObject::factory('core_page');
169             $core->query("UPDATE core_page set is_system_page = 0, element_id= 0 WHERE element_id = {$t->id}");
170             
171         }
172         
173         return $ret;
174         
175     }
176     
177     function syncTemplateFromPage($pgdata)
178     {
179         //print_r($pgdata);
180         if (empty($pgdata['page'])) {
181             return false;
182         }
183         $element_id = DB_DataObject::factory('core_enum')->lookup('core_page_type', 'element'); 
184
185         $core = DB_DataObject::factory('core_page');
186         //DB_DataObject::DebugLevel(1);
187         $core->setFrom(array(
188                 'page_type_id'  => $element_id,
189                 'page_link' => $this->name,
190                 'parent_id' => $pgdata['page']->id,
191                 'is_element' => 1,
192                 'element_id' => $this->id,
193                 'language' => 'en',
194                 'is_system_page' => 1
195         ));
196         if ($core->count()) {
197             $core->fetch();
198             $this->page = clone($core);
199             return;
200         }
201         
202         
203         $core = DB_DataObject::factory('core_page');
204         $core->title = $this->name; /// placeholder..
205          
206        // from parsing earlier..
207         
208         //print_R($this->content_strings);
209         
210         // allow for bodyToDisplayHTML... etc..
211         foreach( array('title', 'body', 'extended') as $prop) {
212             if (isset($this->content_strings[$prop])) {
213                 continue;
214             }
215             foreach($this->content_strings as $k=>$v) {
216                 if (substr($k,0,strlen($prop)) == $prop) {
217                     $this->content_strings[$prop] = $v;
218                 }
219                 
220             }
221         }
222         $core->setFrom($this->content_strings); 
223         
224         $core->setFrom(array(
225                 'page_type_id'  => $element_id,
226                 'page_link' => $this->name,
227                 'parent_id' => $pgdata['page']->id,
228                 'is_element' => 1,
229                 'element_id' => $this->id,
230                 'language' => 'en',
231                 'is_system_page' => 1
232         ));
233         $core->insert();    
234         
235         $this->page = clone($core);
236     }
237     
238     
239 }
240