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');
96 $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
97 return $ret; // something went wrong!?
100 while (($fn = readdir($dh)) !== false) {
102 if(empty($fn) || $fn[0] == '.') {
105 $fp = $dir . '/'. $fn;
113 if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
114 $temp = $tp->syncJsWords(array(
116 'template_dir' => $dir,
124 if (preg_match('/\.php$/', $fn)) {
125 $temp = $tp->syncPhpGetText(array(
127 'template_dir' => $dir,
136 if (!preg_match('/\.bjs$/', $fn)){
139 // var_dump($fn);exit;
141 echo "Processing {$fn} \n";
147 $template = DB_DataObject::factory('cms_template');
148 $template->setFrom(array(
156 if($template->find(true)){
157 $o = clone ($template);
159 $template->filetype = 'bjs';
161 $template->updated = $template->sqlValue("NOW()");
164 $ids[] = $template->insert() ;
166 $template->update($o);
172 $data = json_decode(file_get_contents($base . '/' . $fn), true);
174 $template->words = empty($data['strings']) ? array() : $data['strings'];
176 $x = DB_DataObject::Factory('cms_templatestr');
177 $x->syncTemplateWords($template, false);
181 $del = DB_DataObject::factory('cms_template');
182 $del->whereAddIn('!id', $ids, 'int');
183 $del->view_name = $base;
184 $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
185 $delids = $del->fetchAll('id');
187 DB_DataObject::factory('core_template')->query(
188 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
194 function scanPowerpoint()
196 $ff = HTML_FlexyFramework::get();
200 if(!isset($ff->Pman_Cms['powerpoint'])) {
203 foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
204 $strings = $this->scanPowerpointText($dir);
206 foreach($strings as $slide => $words) {
207 $ct = DB_DataObject::factory('cms_template');
208 $temp = $ct->syncPowerpointXMLText(array(
209 'template' => $slide . '.xml',
210 'template_dir' => $dir . '/ppt/slides',
220 $del = DB_DataObject::factory('cms_template');
221 $del->whereAddIn('!id', $ids, 'int');
222 $del->view_name = $base;
223 $del->filetype = 'xml';
224 $delids = $del->fetchAll('id');
226 DB_DataObject::factory('core_template')->query(
227 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
236 * scan translatable text items in a powerpoint folder
237 * translatable text items should have names starting with 'tr-'
239 * @param String $path path of the powerpoint folder to be scanned
240 * @return Array $strings 2d array containing translatable strings grouped by slide name
243 static function scanPowerpointText($path)
246 // get id of slides from presentation xml
247 $slideRids = array();
249 $xmlString = file_get_contents($path . '/ppt/presentation.xml');
250 $sxe = new SimpleXMLElement($xmlString);
251 $slides = $sxe->xpath('./p:sldIdLst/p:sldId');
253 foreach($slides as $slide) {
254 $slideRids[] = $slide->attributes('r', true)->id->__toString();
258 // get id and name of slides from presentation xml rels
259 $slideNames = array();
261 $xmlString = file_get_contents($path . '/ppt/_rels/presentation.xml.rels');
262 $sxe = new SimpleXMLElement($xmlString);
263 $relationships = $sxe->xpath('.')[0];
265 foreach($relationships->children() as $relationship) {
267 if($relationship->attributes()->Type->__toString() == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide") {
268 $target = $relationship->attributes()->Target->__toString();
269 $slideName = substr(substr($target, 0 , strlen($target) - strlen('.xml')), strlen('slides/'));
270 $slideNames[$relationship->attributes()->Id->__toString()] = $slideName;
274 // get strings from slides
277 foreach($slideRids as $slideRid) {
278 $slide = $slideNames[$slideRid];
279 $strings[$slide] = array();
281 $xmlString = file_get_contents($path . '/ppt/slides/' . $slide . '.xml');
282 $sxe = new SimpleXMLElement($xmlString);
283 $texts = $sxe->xpath('./p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:t');
285 foreach($texts as $text) {
286 $property = $text->xpath('../../../../p:nvSpPr/p:cNvPr');
288 // cannot find property (It won't happen in theory)
289 if(empty($property)) {
293 $textName = $property[0]->attributes()->name->__toString();
295 $textValue = $text->__toString();
297 // a text is translatable if its name starts with 'tr-'
298 if(strpos($textName, 'tr-') === 0 && !in_array($textValue, $strings[$slide])) {
299 $strings[$slide][] = $textValue;
308 function scanTables()
310 $ff = HTML_FlexyFramework::get();
313 if(!isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
316 $cts = DB_DataObject::factory('cms_templatestr');
319 echo "Sync tables.....\n";
322 foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
323 $t = DB_DataObject::factory($table);
324 foreach($t->fetchAll() as $d) {
325 $cts->onTableChange($this, $d, 'update');
331 function syncLanguage()
333 $ff = HTML_FlexyFramework::get();
335 if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
336 $this->jerr("config[Pman_Cms][languages] is not set");
341 echo "Sync the Languages template.....\n";
344 foreach($ff->Pman_Cms['languages'] as $l) {
346 echo "Sync $l Language.....\n";
349 $tps = DB_DataObject::factory('cms_templatestr');
350 $tps->syncLang($l); /// this should be configured somewhere..