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'));
47 if (empty($ff->Pman_Cms)) {
48 $this->jerr("config[Pman_Cms] is not set");
50 $this->scanPowerpoint();
53 $this->syncLanguage();
62 $ff = HTML_FlexyFramework::get();
67 foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
68 $this->scanFilesBase($base);
73 * this does all the scan work.
74 * it's pretty simple (does not do subdirectories...)
76 * only supports BJS / PHP / JS currently.
81 function scanFilesBase($base)
84 $ff = HTML_FlexyFramework::get();
86 $dir = $ff->Pman_Cms['site_dir'][$base];
90 $tp = DB_DAtaObject::Factory('cms_template');
95 $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
96 return $ret; // something went wrong!?
99 while (($fn = readdir($dh)) !== false) {
101 if(empty($fn) || $fn[0] == '.') {
104 $fp = $dir . '/'. $fn;
112 if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
113 $temp = $tp->syncJsWords(array(
115 'template_dir' => $dir,
123 if (preg_match('/\.php$/', $fn)) {
124 $temp = $tp->syncPhpGetText(array(
126 'template_dir' => $dir,
135 if (!preg_match('/\.bjs$/', $fn)){
138 // var_dump($fn);exit;
140 echo "Processing {$fn} \n";
146 $template = DB_DataObject::factory('cms_template');
147 $template->setFrom(array(
155 if($template->find(true)){
156 $o = clone ($template);
158 $template->filetype = 'bjs';
160 $template->updated = $template->sqlValue("NOW()");
163 $ids[] = $template->insert() ;
165 $template->update($o);
171 $data = json_decode(file_get_contents($base . '/' . $fn), true);
173 $template->words = empty($data['strings']) ? array() : $data['strings'];
175 $x = DB_DataObject::Factory('cms_templatestr');
176 $x->syncTemplateWords($template, false);
180 $del = DB_DataObject::factory('cms_template');
181 $del->whereAddIn('!id', $ids, 'int');
182 $del->view_name = $base;
183 $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
184 $delids = $del->fetchAll('id');
186 DB_DataObject::factory('core_template')->query(
187 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
193 function scanPowerpoint()
195 $ff = HTML_FlexyFramework::get();
199 if(!isset($ff->Pman_Cms['powerpoint'])) {
202 foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
203 $strings = $this->scanPowerpointText($dir);
205 foreach($strings as $slide => $words) {
206 $ct = DB_DataObject::factory('cms_template');
207 $temp = $ct->syncPowerpointXMLText(array(
208 'template' => $slide . '.xml',
209 'template_dir' => $dir . '/ppt/slides',
219 $del = DB_DataObject::factory('cms_template');
220 $del->whereAddIn('!id', $ids, 'int');
221 $del->view_name = $base;
222 $del->filetype = 'xml';
223 $delids = $del->fetchAll('id');
225 DB_DataObject::factory('core_template')->query(
226 'update cms_template set is_deleted = 1 where id in('. implode(',', $delids). ')'
235 * scan translatable text items in a powerpoint folder
236 * translatable text items should have names starting with 'tr-'
238 * @param String $path path of the powerpoint folder to be scanned
239 * @return Array $strings 2d array containing translatable strings grouped by slide name
242 static function scanPowerpointText($path)
245 // get id of slides from presentation xml
246 $slideRids = array();
248 $xmlString = file_get_contents($path . '/ppt/presentation.xml');
249 $sxe = new SimpleXMLElement($xmlString);
250 $slides = $sxe->xpath('./p:sldIdLst/p:sldId');
252 foreach($slides as $slide) {
253 $slideRids[] = $slide->attributes('r', true)->id->__toString();
257 // get id and name of slides from presentation xml rels
258 $slideNames = array();
260 $xmlString = file_get_contents($path . '/ppt/_rels/presentation.xml.rels');
261 $sxe = new SimpleXMLElement($xmlString);
262 $relationships = $sxe->xpath('.')[0];
264 foreach($relationships->children() as $relationship) {
266 if($relationship->attributes()->Type->__toString() == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide") {
267 $target = $relationship->attributes()->Target->__toString();
268 $slideName = substr(substr($target, 0 , strlen($target) - strlen('.xml')), strlen('slides/'));
269 $slideNames[$relationship->attributes()->Id->__toString()] = $slideName;
273 // get strings from slides
276 foreach($slideRids as $slideRid) {
277 $slide = $slideNames[$slideRid];
278 $strings[$slide] = array();
280 $xmlString = file_get_contents($path . '/ppt/slides/' . $slide . '.xml');
281 $sxe = new SimpleXMLElement($xmlString);
282 $texts = $sxe->xpath('./p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:t');
284 foreach($texts as $text) {
285 $property = $text->xpath('../../../../p:nvSpPr/p:cNvPr');
287 // cannot find property (It won't happen in theory)
288 if(empty($property)) {
292 $textName = $property[0]->attributes()->name->__toString();
294 $textValue = $text->__toString();
296 // a text is translatable if its name starts with 'tr-'
297 if(strpos($textName, 'tr-') === 0 && !in_array($textValue, $strings[$slide])) {
298 $strings[$slide][] = $textValue;
307 function scanTables()
309 $ff = HTML_FlexyFramework::get();
312 if(!isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
315 $cts = DB_DataObject::factory('cms_templatestr');
318 echo "Sync tables.....\n";
321 foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
322 $t = DB_DataObject::factory($table);
323 foreach($t->fetchAll() as $d) {
324 $cts->onTableChange($this, $d, 'update');
330 function syncLanguage()
332 $ff = HTML_FlexyFramework::get();
334 if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
335 $this->jerr("config[Pman_Cms][languages] is not set");
340 echo "Sync the Languages template.....\n";
343 foreach($ff->Pman_Cms['languages'] as $l) {
345 echo "Sync $l Language.....\n";
348 $tps = DB_DataObject::factory('cms_templatestr');
349 $tps->syncLang($l); /// this should be configured somewhere..