3 * Table Definition for core_template
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
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
12 class Pman_Core_DataObjects_Core_template extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
17 public $__table = 'core_template'; // table name
18 public $id; // int(11) not_null primary_key auto_increment
19 public $template; // string(64) not_null
21 public $updated; // blob(65535) blob
22 public $lang; // text NOT NULL;
23 public $view_name; // eg mobile or desktop
24 /* the code above is auto generated do not remove the tag below */
28 function applyFilters($q, $au, $roo)
30 //DB_DataObject::debugLEvel(1);
31 // $x = DB_Dataobject::Factory($this->tableName());
34 // template scanning and syncing should be done by the UpdateDatabase Code.
35 //if (!$x->count() || !empty($q['_rescan'])) {
36 //DB_DataObject::debugLEvel(1);
37 //$tp = DB_DataObject::factory('core_template');
38 //$opts = HTML_FlexyFramework::get()->Pman_Core;
39 //$tp->syncTemplateDir(false, '', !empty($q['_rescan']));
40 //if (isset($q['lang'])) {
41 // $this->syncLang($q['lang']);
50 function toRooArray($req)
52 $ret = $this->toArray();
53 if (!empty($req['_clean_name']) ) {
54 $ret['template_clean'] = preg_replace('#\.html$#i', '', $this->template);
62 * @param base (should be full path to template directory)
63 * @param subdir = empty for top or subpath.
65 function syncTemplateDir($base = false, $subdir = '', $force = false)
67 echo "syncTemplateDir: $base , $subdir, $force \n";
68 //print_r(func_get_args());
70 $ff = HTML_FlexyFramework::get();
71 if (!isset($ff->Pman_Core)) {
72 echo "[ERROR] Not scanning template directory - no set in Pman_Core[templateDir]\n";
75 $opts = $ff->Pman_Core;
76 if (is_array($opts['templateDir'])) {
77 foreach($opts['templateDir'] as $type=>$dir) {
78 $this->syncTemplateDir($dir, '', $force);
83 $base = $opts['templateDir'];
88 $tmpls = DB_DataObject::factory('core_template');
89 $this->tmpls = $tmpls->fetchAll('template','id'); // dupes??
92 $tmp_dir = $base . (empty($subdir) ? '' : '/') . $subdir;
94 if(!is_dir($tmp_dir)){
98 $dh = opendir($tmp_dir);
100 return; // something went wrong!?
103 while (($fn = readdir($dh)) !== false) {
104 // do we care that it will try and parse the template directory??? - not really..
105 // as we are only looking for php files..
106 if(empty($fn) || $fn[0] == '.'){
110 $fullpath = $tmp_dir."/".$fn;
111 // echo "filename: ".$fullpath." \n";
113 $relpath = $subdir . (empty($subdir) ? '' : '/') . $fn;
115 if(is_dir($fullpath)){
116 // then recursively call self...
118 // var_Dump($subdir . (empty($subdir) ? '' : '/') . $fn);
119 $this->syncTemplateDir($base, $subdir . (empty($subdir) ? '' : '/') . $fn );
123 if (!preg_match('/(\.html|\.txt|\.abw)$/', $fn)) {
129 // var_dump($tmp_path);
130 // $fn = basename($fn);
131 if (isset($this->tmpls[$relpath])) {
132 unset($this->tmpls[$relpath]);
138 $template = $this->syncTemplate($relpath, true, false);
139 // var_dump($template);
140 if (is_a($template, 'PEAR_Error')) {
148 foreach($this->tmpls as $id) {
149 $x = DB_DataObject::factory($this->tableName());
157 /* compile a html template
159 * @param template_dir << the path to the template dir ... Pman/XXX/template ...
160 * @param template << name of template used by name field)
161 * @param base << view name (module ? + templates?)
165 function syncTemplatePage($pgdata)
170 //echo "compiling:"; print_r($pgdata);
171 // read the template and extract the translatable strings.
172 ini_set('memory_limit', '512M');
175 $n= $pgdata['template']; // remove trailing slash..
177 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
178 $opts = HTML_FlexyFramework::get()->Pman_Core;
180 //$dir = $opts['templateDir'] . '/' . $node;
182 'fatalError' => PEAR_ERROR_EXCEPTION,
183 'disableTranslate' => false,
184 'templateDir' => $pgdata['template_dir'],
185 'compileDir' => $fopts['compileDir'] . '_translation_files',
186 'forceCompile' => true, //?? only for force above???
189 // non-html templates - treat as such..
190 // abiword - treat as html?
191 if (!preg_match('/\.(html|abw)$/i', $pgdata['template'])) {
192 $oo['nonHTML'] = true;
195 //print_r(array($oo, $n));
198 $flexy = new HTML_Template_Flexy( $oo );
200 if (!$flexy->resolvePath ($pgdata['template'])) {
201 //echo "SKIP - could not resolve path?\n"; print_r($oo);
205 // attempt to find the template... record.
206 $tmpl = DB_DataObject::Factory($this->tableName());
208 $tmpl->view_name = $pgdata['base'];
209 if ($tmpl->get('template', $pgdata['template'])) {
210 if (strtotime($tmpl->updated) >= filemtime($flexy->resolvePath ($pgdata['template']))) {
211 if ($tmpl->is_deleted != 0 || $tmpl->filetype != 'html') {
213 $tmpl->is_deleted = 0;
214 $tmpl->filetype = 'html';
227 $r = $flexy->compile($pgdata['template']);
228 } catch(Exception $e) {
230 $tmpl->updated = date('Y-m-d H:i:s',filemtime($flexy->resolvePath ($pgdata['template'])));
232 $tmpl->is_deleted = 0;
233 $tmpl->filetype = 'html';
234 $tmpl->update($tmpl);
236 $tmpl->is_deleted = 0;
237 $tmpl->filetype = 'html';
247 if (is_a($r,'PEAR_Error')) {
249 // echo $r->toString(). "\n";
253 $tmpl = DB_DataObject::Factory($this->tableName());
254 $tmpl->words = file_exists($flexy->getTextStringsFile) ?
255 unserialize(file_get_contents($flexy->getTextStringsFile)) :
258 $tmpl->contentStrings = $flexy->compiler->contentStrings;
259 //var_dump(file_exists($flexy->getTextStringsFile));
260 //print_r($tmpl->words);
261 $tmpl->currentTemplate = $flexy->currentTemplate;
263 $tmpl->view_name = $pgdata['base'];
266 if (!$tmpl->get('template', $pgdata['template'])) {
267 $tmpl->is_deleted = 0;
268 $tmpl->filetype = 'html';
269 $tmpl->template = $pgdata['template'];
270 $tmpl->lang = 'en'; /// ??? hard coded??
271 $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
275 // has it been cahnged...
276 //if (!$force && filemtime($flexy->currentTemplate) == strtotime($tmpl->updated)) {
280 if (empty($tmpl->lang)) {
281 //echo "FIX LANG?";exit;
282 $tmpl->lang = 'en'; /// ??? hard coded??
284 $tmpl->filetype = 'html';
285 $tmpl->is_deleted = 0;
286 $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
291 $x = DB_DataObject::Factory('core_templatestr');
292 $x->syncTemplateWords($tmpl);
294 // if file_exists ( template/path/name.php << eg. a matching view..)
295 // then create a system page for this.
299 $x = DB_DataObject::Factory('core_template_element');
300 $tmpl->elements = $x->syncTemplateElement($tmpl,
301 file_get_contents($flexy->currentTemplate),
302 $flexy->compiler->contentStrings,
310 function syncPhpGetText($pgdata)
312 $tmpl = DB_DataObject::Factory($this->tableName());
313 $tmpl->view_name = $pgdata['base'];
314 $tmpl->currentTemplate = $pgdata['template_dir'] . '/'. $pgdata['template'];
316 if ($tmpl->get('template', $pgdata['template'])) {
317 if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
318 if ($tmpl->is_deleted != 0 || $tmpl->filetype != 'html') {
320 $tmpl->is_deleted = 0;
321 $tmpl->filetype = 'php';
329 $ar = token_get_all(file_get_contents( $tmpl->currentTemplate ));
330 foreach( $ar as $i=> $tok) {
331 if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
337 if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
340 if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
344 $words[] = str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
347 // create the template...
352 $tmpl->template = $pgdata['template'];
353 $tmpl->lang = 'en'; /// ??? hard coded??
354 $tmpl->filetype = 'php';
355 $tmpl->is_deleted = 0;
356 $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
360 $tmpl->filetype = 'php';
361 $tmpl->is_deleted = 0;
362 $tmpl->lang = 'en'; /// ??? hard coded??
363 $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
367 $words = array_unique($words);
369 if (!count($words)) {
374 $tmpl->words = $words;
376 $x = DB_DataObject::Factory('core_templatestr');
377 $x->syncTemplateWords($tmpl);
388 REPLACE(view_name, '.','_'),
390 REPLACE(template,'/','_')
396 = 'release_pressrelease_distributionreportnew_journalistdistribution.php'
401 function genGetText($clsname, $lang=false)
403 static $done = false;
404 $clsname = strtolower($clsname);
406 textdomain($clsname);
409 $ff = HTML_FlexyFramework::get();
410 $lang = $lang ? $lang : (isset($ff->locale) ? $ff->locale : 'en');
413 if (!empty($done[$clsname.':'.$lang])) {
414 return; // already sent headers and everything.
417 putenv("LANGUAGE=$lang");
418 setlocale(LC_ALL, $lang);
419 $d = DB_DataObject::factory($this->tableName());
423 REPLACE(view_name, '.','_'),
425 REPLACE(template,'/','_')
429 $d->filetype = 'php';
430 if (! $d->find(true) ){
431 $done[$clsname.':'.$lang] = true;
434 $user = 'www-data'; // ?? do we need other ones
435 $compileDir = ini_get('session.save_path') .'/' .
436 $user . '_gettext_' . $ff->project;
438 if ($ff->appNameShort) {
439 $compileDir .= '_' . $ff->appNameShort;
442 $compileDir .= '.' . $ff->version;
444 $lang = $lang ? $lang : $ff->locale;
445 $fdir = "{$compileDir}/{$lang}/LC_MESSAGES";
446 $fname = "{$fdir}/{$clsname}.mo";
450 bindtextdomain($clsname, $compileDir) ;
451 bind_textdomain_codeset($clsname, 'UTF-8');
453 textdomain($clsname);
456 //textdomain($clsname);
458 $done[$clsname.':'.$lang] = 1;
460 // do we need to compile the file..
461 $ts = DB_DataObject::Factory('core_templatestr');
462 $ts->selectAdd('MAX(updated) as updated');
464 $ts->template_id = $d->id;
465 if (!$ts->find(true)) {
466 // then in theory there are no translations
469 if (file_exists($fname) && strtotime($ts->updated) < filemtime($fname)) {
470 return; // file exists and is newer than our updated line.
472 //DB_DataObject::debugLevel(1);
474 $ts = DB_DataObject::Factory('core_templatestr');
476 $ts->selectAdd('join_src_id_id.txt as src_id_txt, core_templatestr.txt as txt');
478 $ts->template_id = $d->id;
479 $words = $ts->fetchAll('src_id_txt', 'txt' );
481 if (!file_exists($fdir)) {
483 mkdir($fdir, 0700, true);
486 require_once 'File/Gettext.php';
487 $gt = File_Gettext::factory('PO', preg_replace('/\.mo$/', '.po', $fname));
493 'Content-Type' => 'text/plain; charset=UTF-8',
494 'Content-Transfer-Encoding' => ' 8bit',
495 'PO-Revision-Date' => date('Y-m-d H:iO'),
504 require_once 'System.php';
505 $poname = preg_replace('/\.mo$/', '.po', $fname);
506 $msgfmt = System::which('msgfmt');
507 $cmd = "{$msgfmt} {$poname} -o {$fname}";
517 require_once 'File/Gettext.php';
518 $gt = File_Gettext::factory('MO', $fname);
524 'Content-Type' => 'text/plain; charset=UTF-8',
525 'Content-Transfer-Encoding' => ' 8bit',
526 'PO-Revision-Date' => date('Y-m-d H:iO'),