5 * simlar to Pman/Admin/UpdateBjsTemplates...
8 * scans JS (for ._("...") )
13 require_once 'Pman/Admin/UpdateBjsTemplates.php';
14 class Pman_Cms_UpdateBjsTemplates extends Pman_Admin_UpdateBjsTemplates
17 static $cli_desc = "Update BJS Templates";
19 static $cli_opts = array();
28 $ff = HTML_FlexyFramework::get();
29 if (!empty($ff->cli)) {
37 function get($tbl, $opts=array())
39 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
46 $ff = HTML_FlexyFramework::get();
48 if (empty($ff->Pman_Cms)) {
49 $this->jerr("config[Pman_Cms] is not set");
51 $this->scanPowerpoint();
54 $this->syncLanguage();
63 $ff = HTML_FlexyFramework::get();
68 foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
69 $this->scanFilesBase($base);
74 * this does all the scan work.
75 * it's pretty simple (does not do subdirectories...)
77 * only supports BJS / PHP / JS currently.
82 function scanFilesBase($base)
85 $ff = HTML_FlexyFramework::get();
87 $dir = $ff->Pman_Cms['site_dir'][$base];
91 $tp = DB_DAtaObject::Factory('cms_template');
98 $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
99 return $ret; // something went wrong!?
102 while (($fn = readdir($dh)) !== false) {
104 if(empty($fn) || $fn[0] == '.') {
107 $fp = $dir . '/'. $fn;
115 if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
116 $temp = $tp->syncJsWords(array(
118 'template_dir' => $dir,
126 if (preg_match('/\.php$/', $fn)) {
127 $temp = $tp->syncPhpGetText(array(
129 'template_dir' => $dir,
138 if (!preg_match('/\.bjs$/', $fn)){
141 // var_dump($fn);exit;
143 echo "Processing {$fn} \n";
149 $template = DB_DataObject::factory('cms_template');
150 $template->setFrom(array(
158 if($template->find(true)){
159 $o = clone ($template);
161 $template->filetype = 'bjs';
163 $template->updated = $template->sqlValue("NOW()");
166 $ids[] = $template->insert() ;
168 $template->update($o);
174 $data = json_decode(file_get_contents($base . '/' . $fn), true);
176 $template->words = empty($data['strings']) ? array() : $data['strings'];
178 $x = DB_DataObject::Factory('cms_templatestr');
179 $x->syncTemplateWords($template, false);
183 $del = DB_DataObject::factory('cms_template');
184 $del->whereAddIn('!id', $ids, 'int');
185 $del->view_name = $base;
186 $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
187 $delids = $del->fetchAll('id');
189 DB_DataObject::factory('core_template')->query(
190 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
196 function scanPowerpoint()
198 $ff = HTML_FlexyFramework::get();
202 if(!isset($ff->Pman_Cms['powerpoint'])) {
205 foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
206 $strings = $this->scanPowerpointText($dir);
208 foreach($strings as $slide => $words) {
209 $ct = DB_DataObject::factory('cms_template');
210 $temp = $ct->syncPowerpointXMLText(array(
211 'template' => $slide . '.xml',
212 'template_dir' => $dir . '/ppt/slides',
222 $del = DB_DataObject::factory('cms_template');
223 $del->whereAddIn('!id', $ids, 'int');
224 $del->view_name = $base;
225 $del->filetype = 'xml';
226 $delids = $del->fetchAll('id');
228 DB_DataObject::factory('core_template')->query(
229 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
238 * scan translatable text items in a powerpoint folder
239 * translatable text items should have names starting with 'tr-'
241 * @param String $path path of the powerpoint folder to be scanned
242 * @return Array $strings 2d array containing translatable strings grouped by slide name
245 static function scanPowerpointText($path)
248 // get id of slides from presentation xml
249 $slideRids = array();
251 $xmlString = file_get_contents($path . '/ppt/presentation.xml');
252 $sxe = new SimpleXMLElement($xmlString);
253 $slides = $sxe->xpath('./p:sldIdLst/p:sldId');
255 foreach($slides as $slide) {
256 $slideRids[] = $slide->attributes('r', true)->id->__toString();
260 // get id and name of slides from presentation xml rels
261 $slideNames = array();
263 $xmlString = file_get_contents($path . '/ppt/_rels/presentation.xml.rels');
264 $sxe = new SimpleXMLElement($xmlString);
265 $relationships = $sxe->xpath('.')[0];
267 foreach($relationships->children() as $relationship) {
269 if($relationship->attributes()->Type->__toString() == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide") {
270 $target = $relationship->attributes()->Target->__toString();
271 $slideName = substr(substr($target, 0 , strlen($target) - strlen('.xml')), strlen('slides/'));
272 $slideNames[$relationship->attributes()->Id->__toString()] = $slideName;
276 // get strings from slides
279 foreach($slideRids as $slideRid) {
280 $slide = $slideNames[$slideRid];
281 $strings[$slide] = array();
283 $xmlString = file_get_contents($path . '/ppt/slides/' . $slide . '.xml');
284 $sxe = new SimpleXMLElement($xmlString);
285 $texts = $sxe->xpath('./p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:t');
287 foreach($texts as $text) {
288 $property = $text->xpath('../../../../p:nvSpPr/p:cNvPr');
290 // cannot find property (It won't happen in theory)
291 if(empty($property)) {
295 $textName = $property[0]->attributes()->name->__toString();
297 $textValue = $text->__toString();
299 // a text is translatable if its name starts with 'tr-'
300 if(strpos($textName, 'tr-') === 0 && !in_array($textValue, $strings[$slide])) {
301 $strings[$slide][] = $textValue;
310 function scanTables()
312 $ff = HTML_FlexyFramework::get();
315 if(!isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
318 $cts = DB_DataObject::factory('cms_templatestr');
321 echo "Sync tables.....\n";
324 foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
325 $t = DB_DataObject::factory($table);
326 foreach($t->fetchAll() as $d) {
327 $cts->onTableChange($this, $d, 'update');
333 function syncLanguage()
335 $ff = HTML_FlexyFramework::get();
337 if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
338 $this->jerr("config[Pman_Cms][languages] is not set");
343 echo "Sync the Languages template.....\n";
346 foreach($ff->Pman_Cms['languages'] as $l) {
348 echo "Sync $l Language.....\n";
351 $tps = DB_DataObject::factory('cms_templatestr');
352 $tps->syncLang($l); /// this should be configured somewhere..