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 $this->scanPowerpoint();
49 $this->syncLanguage();
58 $ff = HTML_FlexyFramework::get();
60 if (empty($ff->Pman_Cms)) {
61 $this->jerr("config[Pman_Cms] is not set");
65 foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
66 $this->scanFilesBase($base);
71 * this does all the scan work.
72 * it's pretty simple (does not do subdirectories...)
74 * only supports BJS / PHP / JS currently.
79 function scanFilesBase($base)
82 $ff = HTML_FlexyFramework::get();
84 $dir = $ff->Pman_Cms['site_dir'][$base];
88 $tp = DB_DAtaObject::Factory('cms_template');
93 $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
94 return $ret; // something went wrong!?
97 while (($fn = readdir($dh)) !== false) {
99 if(empty($fn) || $fn[0] == '.') {
102 $fp = $dir . '/'. $fn;
110 if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
111 $temp = $tp->syncJsWords(array(
113 'template_dir' => $dir,
121 if (preg_match('/\.php$/', $fn)) {
122 $temp = $tp->syncPhpGetText(array(
124 'template_dir' => $dir,
133 if (!preg_match('/\.bjs$/', $fn)){
136 // var_dump($fn);exit;
138 echo "Processing {$fn} \n";
144 $template = DB_DataObject::factory('cms_template');
145 $template->setFrom(array(
153 if($template->find(true)){
154 $o = clone ($template);
156 $template->filetype = 'bjs';
158 $template->updated = $template->sqlValue("NOW()");
161 $ids[] = $template->insert() ;
163 $template->update($o);
169 $data = json_decode(file_get_contents($base . '/' . $fn), true);
171 $template->words = empty($data['strings']) ? array() : $data['strings'];
173 $x = DB_DataObject::Factory('cms_templatestr');
174 $x->syncTemplateWords($template, false);
178 $del = DB_DataObject::factory('cms_template');
179 $del->whereAddIn('!id', $ids, 'int');
180 $del->view_name = $base;
181 $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
182 $delids = $del->fetchAll('id');
184 DB_DataObject::factory('core_template')->query(
185 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
191 function scanPowerpoint()
193 $ff = HTML_FlexyFramework::get();
195 if (empty($ff->Pman_Cms)) {
196 $this->jerr("config[Pman_Cms] is not set");
199 if(isset($ff->Pman_Cms['powerpoint'])) {
200 foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
201 $strings = $this->scanPowerpointText($dir);
203 foreach($strings as $slide => $words) {
204 $ct = DB_DataObject::factory('cms_template');
205 $temp = $ct->syncPowerpointXMLText(array(
206 'template' => $slide . '.xml',
207 'template_dir' => $dir . '/ppt/slides',
217 $del = DB_DataObject::factory('cms_template');
218 $del->whereAddIn('!id', $ids, 'int');
219 $del->view_name = $base;
220 $del->filetype = 'xml';
221 $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();
312 if (empty($ff->Pman_Cms)) {
313 $this->jerr("config[Pman_Cms] is not set");
316 if(isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
317 $cts = DB_DataObject::factory('cms_templatestr');
320 echo "Sync tables.....\n";
323 foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
324 $t = DB_DataObject::factory($table);
325 foreach($t->fetchAll() as $d) {
326 $cts->onTableChange($this, $d, 'update');
332 function syncLanguage()
334 $ff = HTML_FlexyFramework::get();
336 if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
337 $this->jerr("config[Pman_Cms][languages] is not set");
342 echo "Sync the Languages template.....\n";
345 foreach($ff->Pman_Cms['languages'] as $l) {
347 echo "Sync $l Language.....\n";
350 $tps = DB_DataObject::factory('cms_templatestr');
351 $tps->syncLang($l); /// this should be configured somewhere..