Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
SkinTool.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 
22 import( 'Smarty/Smarty.class.php');
23 import( 'Dataface/LanguageTool.php');
81 class Dataface_SkinTool extends Smarty{
82 
83 
85  var $ENV;
86  var $skins = array();
87  var $locals = array();
89  var $app;
90  var $resultController = null;
91 
92  function Dataface_SkinTool() {
93 
94  if ( is_writable($GLOBALS['Dataface_Globals_Local_Templates_c']) ){
95 
96  $this->compile_dir = $GLOBALS['Dataface_Globals_Local_Templates_c'];
97  } else if ( is_writable($GLOBALS['Dataface_Globals_Templates_c'])){
98  $this->compile_dir = $GLOBALS['Dataface_Globals_Templates_c'];
99  } else {
100  throw new Exception("<h1>No appropriate directory could be found to save
101  Dataface's compiled templates.</h1>
102 
103  <p>Dataface uses the Smarty Template engine for its templates, which compiles
104  templates and stores them on the server for improved performance. You can
105  either store these templates in the Dataface directory or your application's
106  directory.</p>
107 
108  <p>To store the templates in the Dataface directory, please ensure that the
109  <pre>$GLOBALS[Dataface_Globals_Templates_c]</pre> directory exists and is writable by the
110  web server. </p>
111  <p>You can make it writable by the web server on most unix and linux systems,
112  by issuing the following command in the shell:
113  <code><pre>chmod 777 $GLOBALS[Dataface_Globals_Templates_c] </pre></code>.</p>
114 
115  <p>To store the templates in your application's directory, please ensure
116  that the <pre>$GLOBALS[Dataface_Globals_Local_Templates_c]</pre> directory exists and is
117  writable by the web server.</p>", E_USER_ERROR);
118  }
119 
120  $this->languageTool =& Dataface_LanguageTool::getInstance();
121 
122 
123  $this->register_skin('dataface', $GLOBALS['Dataface_Globals_Templates']);
124 
125  $this->register_skin('default', $GLOBALS['Dataface_Globals_Local_Templates']);
126  $this->register_plugins($GLOBALS['Dataface_Globals_Local_Plugins']);
127 
129  $this->app =& $app;
130  $conf =& $app->_conf;
131  $resultSet =& $app->getResultSet();
132  if ( isset( $conf['auto_load_results'] ) and $conf['auto_load_results'] ){
133  $currentRecord =& $resultSet->loadCurrent();
134  } else {
135  $currentRecord = null;
136  }
137 
138  if ( isset($app->_conf['_themes']) and is_array($app->_conf['_themes']) ){
139  foreach ( $app->_conf['_themes'] as $themename=>$themepath ){
140  $this->register_skin($themename, $themepath.'/templates');
141  }
142  }
143 
144  $this->ENV = array(
145  'REQUEST' => &$_REQUEST,
146  'SESSION' => &$_SESSION,
147  'DATAFACE_PATH' => DATAFACE_PATH,
148  'DATAFACE_URL' => DATAFACE_URL,
149  'DATAFACE_SITE_PATH' => DATAFACE_SITE_PATH,
150  'DATAFACE_SITE_URL' => DATAFACE_SITE_URL,
151  'DATAFACE_SITE_HREF' => DATAFACE_SITE_HREF,
152  'SCRIPT_NAME' => DATAFACE_SITE_URL.'/'.basename($_SERVER['SCRIPT_NAME']),
153  'APPLICATION' => &$conf,
154  'APPLICATION_OBJECT' => &$app,
155  'SERVER' => &$_SERVER,
156  'QUERY'=>&$app->_query,
157  'action'=>@$app->_query['-action'],
158  'table'=>@$app->_query['-table'],
159  'table_object'=> Dataface_Table::loadTable($app->_query['-table']),
160  'relationship'=>@$app->_query['-relationship'],
161  'limit'=>@$app->_query['-limit'],
162  'start'=>@$app->_query['-start'],
163  'resultSet'=>&$resultSet,
164  'record'=>&$currentRecord,
165  'mode'=>&$app->_query['-mode'],
166  'language'=>$app->_conf['lang'],
167  'prefs'=>&$app->prefs,
168  'search'=>@$_REQUEST['-search']
169 
170  );
171 
172  $authTool =& $app->getAuthenticationTool();
173  if ( isset($authTool) ){
174  $user =& $authTool->getLoggedInUser();
175  if ( isset($user) ){
176  $this->ENV['user'] = &$user;
177  $this->ENV['username'] = $authTool->getLoggedInUsername();
178  }
179  }
180  $context = array();
181  $context['APP'] =& $this->app;
182  $context['ENV'] =& $this->ENV;
183  if ( $del = $app->getDelegate() and method_exists($del, 'getTemplateContext') ){
184  $c =& $del->getTemplateContext();
185  if ( is_array($c) ){
186  foreach ($c as $k=>$v){
187  $context[$k] = $v;
188  }
189  }
190  }
191 
192  $this->assign($context);
193  $this->register_function('load_record', array(&$this, 'load_record'));
194  $this->register_function('group',array(&$this,'group'));
195  $this->register_function('img', array(&$this,'img'));
196  $this->register_function('actions', array(&$this, 'actions'));
197  $this->register_function('actions_menu', array(&$this, 'actions_menu'));
198  $this->register_function('record_actions', array(&$this, 'record_actions'));
199  $this->register_function('record_tabs', array(&$this, 'record_tabs'));
200  $this->register_function('result_controller', array(&$this, 'result_controller'));
201  $this->register_function('result_list', array(&$this,'result_list'));
202  $this->register_function('filters', array(&$this, 'filters'));
203  $this->register_function('related_list',array(&$this,'related_list'));
204  $this->register_function('bread_crumbs', array(&$this, 'bread_crumbs'));
205  $this->register_function('search_form', array(&$this, 'search_form'));
206  $this->register_function('language_selector', array(&$this, 'language_selector'));
207  $this->register_function('next_link', array(&$this, 'next_link'));
208  $this->register_function('prev_link', array(&$this, 'prev_link'));
209  $this->register_function('jump_menu', array(&$this, 'jump_menu'));
210  $this->register_function('limit_field', array(&$this, 'limit_field'));
211  $this->register_function('result_index', array(&$this, 'result_index'));
212  $this->register_function('block', array(&$this, 'block'));
213  $this->register_function('summary_list',array(&$this,'summary_list'));
214  $this->register_function('sort_controller',array(&$this,'sort_controller'));
215  $this->register_function('glance_list', array(&$this,'glance_list'));
216  $this->register_function('record_view', array(&$this,'record_view'));
217  $this->register_function('feed', array(&$this,'feed'));
218  $this->register_function('records', array(&$this, 'records'));
219  $this->register_function('form_context', array(&$this, 'form_context'));
220  $this->register_block('translate', array(&$this, 'translate'));
221  $this->register_block('use_macro',array(&$this,'use_macro'));
222  $this->register_block('define_slot', array(&$this,'define_slot'));
223  $this->register_block('fill_slot', array(&$this,'fill_slot'));
224  $this->register_block('if_allowed', array(&$this, 'if_allowed'));
225  $this->register_block('editable', array(&$this, 'editable'));
226  $this->register_block('abs', array(&$this, 'abs'));
227 
228 
229 
230 
231  }
232 
233 
241  function &getResultController(){
242  if ( !isset($this->resultController) ){
243  import('Dataface/ResultController.php');
244 
245  $query =& $this->app->getQuery();
246 
247  $this->resultController = new Dataface_ResultController($query['-table'], $this->app->db(), DATAFACE_SITE_HREF);
248  }
249 
251  }
252 
262  function _get_compile_path($resource_name)
263  {
264  $params = array('resource_name'=>$resource_name, 'resource_base_path' => $this->template_dir);
265  $name = $this->_parse_resource_name($params);
266  $template_dir = dirname($params['resource_name']);
267  $skin = $this->skins[$template_dir];
268  if ( strlen($skin) > 0 and preg_match('/^[0-9a-zA-Z_]+$/', $skin) ){
269  $compile_dir = $this->compile_dir.'/'.$skin;
270  if ( !file_exists($compile_dir) ){
271  $res = @mkdir($compile_dir);
272 
273  if ( !$res ){
274  echo "<h2>Configuration Required</h2>
275  <p>Dataface was unable to create the directory '$compile_dir'
276  to store compiled template files.</p>
277  <h3>Possible reasons for this:</h3>
278  <ul>
279  <li>The script does not have permission to create the directory.</li>
280  <li>The server is operating in safe mode.</li>
281  </ul>
282  <h3>Possible Solutions for this:</h3>
283  <ul>
284  <li>Make the ".dirname($compile_dir)." writable by the web server. E.g. chmod 0777 ".dirname($compile_dir).".</li>
285  <li>Manually create the '$compile_dir' directory and make it writable by the web server.</li>
286  <li>If none of these solves the problem, visit the Dataface forum
287  at <a href=\"http://xataface.com/forum\">http://xataface.com/forum</a>
288  and ask for help.
289  </li>
290  </ul>
291  ";
292  exit;
293  }
294 
295  }
296  if ( !file_exists($compile_dir) ){
297  error_log("Failed to create compile directory '$compile_dir'");
298  throw new Exception("Failed to compile templates due to a configuration problem. See error log for details.", E_USER_ERROR);
299  }
300  } else {
302  }
303  $fname= $this->_get_auto_filename($compile_dir, $resource_name,
304  $this->_compile_id) . '.php';
305 
306  return $fname;
307  }
308 
309 
320  function display($context, $template=null, $compile_id=null){
321  if ( !is_array($context) ) {
322  return parent::display($context);
323  }
324  $event = new StdClass;
325  $event->context =& $context;
326  Dataface_Application::getInstance()->fireEvent('filterTemplateContext', $event);
327  $this->assign($context);
328  return parent::display($template);
329 
330  }
331 
332 
333 
338  public static function &getInstance(){
339  static $instance = 0;
340  static $count = 0;
341  if ( $count++ < 1 ) {
342 
343  $instance = new Dataface_SkinTool();
344  }
345 
346  return $instance;
347  }
348 
349 
350 
360  function register_skin( $name, $template_dir){
361  if ( !is_array($this->template_dir) ){
362  if ( strlen($this->template_dir) > 0 ){
363  $this->template_dir = array($this->template_dir);
364  } else {
365  $this->template_dir = array();
366  }
367  }
368  array_unshift($this->template_dir, $template_dir);
369  $this->skins[$template_dir] = $name;
370 
371  }
372 
386  function register_plugins( $plugin_dir){
387  if ( !is_array($this->plugins_dir) ){
388  if ( strlen($this->plugins_dir) > 0 ){
389  $this->plugins_dir = array($this->plugins_dir);
390  } else {
391  $this->plugins_dir = array();
392  }
393  }
394  array_unshift($this->plugins_dir, $plugin_dir);
395 
396  }
397 
398 
399  //------------------SMARTY TEMPLATE FUNCTIONS---------------------------
400  // These are functions to be used inside templates to get information
401  // from the database.
402  //
403 
424  function load_record($params, &$smarty){
425  import( 'dataface-public-api.php');
426  if ( empty($params['table']) ){
427  $params['table'] = $this->ENV['table'];
428  }
429 
430  if ( empty($params['var']) ){
431  $params['var'] = null;
432  }
433  $table = $params['table'];
434  unset($params['table']);
435  $varname = $params['var'];
436  unset($params['var']);
437  $vars =& $smarty->get_template_vars();
438 
439  if ( count($params) <= 0 ){
440  if ( !$this->app->recordLoaded() ){
441  $record =& $this->ENV['resultSet']->loadCurrent();
442  } else {
443  $record =& $this->app->getRecord();
444  }
445 
446  } else {
447  $record =& df_get_record($table, $params);
448 
449  }
450  if ( isset($varname) ) $vars[$varname] =& $record;
451  else
452  $vars['ENV']['record'] =& $record;
453 
454 
455  }
456 
457 
458 
459 
460  function record_view($params, &$smarty){
461  import('Dataface/RecordView.php');
462 
463  if ( empty($params['record']) ) $params['record'] =& $this->app->getRecord();
464  if ( empty($params['var']) ) $params['var'] = 'rv';
465 
466  $vars =& $smarty->get_template_vars();
467  $vars[$params['var']] = new Dataface_RecordView($params['record']);
468 
469  }
470 
483  function group($params, &$smarty){
484 
485  import( 'Dataface/Utilities.php');
486  if ( empty($params['from']) ){
487  throw new Exception('group: Please specify a from parameter.', E_USER_ERROR);
488  }
489  if ( empty($params['var']) ){
490  throw new Exception('group: Please specify a var parameter.', E_USER_ERROR);
491  }
492  if ( empty($params['on'])){
493  throw new Exception('group: Please specify a field parameter.', E_USER_ERROR);
494  }
495 
496  if ( !empty($params['order']) ){
497  $order = explode(',',$params['order']);
498  } else {
499  $order = array();
500  }
501 
502  if ( !empty($params['titles']) ){
503  $titles = array_map('trim',explode(';', $params['titles']));
504  $titles2 = array();
505  foreach ($titles as $title){
506  list($titleKey, $titleValue) = array_map('trim',explode(':',$title));
507  $titles2[$titleKey] = $titleValue;
508  }
509  } else {
510  $titles2 = array();
511  }
512 
513  $cats = Dataface_Utilities::groupBy($params['on'], $params['from'], $order, $titles2);
514  $context = array($params['var']=>&$cats);
515  $smarty->assign($context);
516 
517  }
518 
531  function img($params, &$smarty){
532 
533  // We have to have at least the src parameter set
534  if ( !isset( $params['src'] ) ) return '';
535 
536 
537  if ( isset( $params['width'] ) ){
538  $width= '&w='.$params['width'];
539  unset($params['width']);
540  } else {
541  $width = '';
542  }
543 
544  if ( isset( $params['height']) ){
545  $height= '&h='.$params['height'];
546  unset($params['height']);
547  } else {
548  $height = '';
549  }
550 
551  $url = DATAFACE_URL;
552  if ( strlen($url) > 0 and $url{0} != '/' ){
553  $url = DATAFACE_SITE_URL.'/'.$url;
554  } else if ( strlen($url) == 0 ){
555  $url = DATAFACE_SITE_URL;
556  } else {
557  $url = '';
558  }
559  $src = $_SERVER['HOST_URI'].$url.'/lib/phpThumb/phpThumb.php?'.$width.$height.'&src='.urlencode($params['src']);
560  unset($params['src']);
561 
562 
563 
564  $tag = "<img src=\"$src\" ";
565  foreach ( array_keys($params) as $key){
566  $tag .= $key.'="'.$params[$key].'" ';
567  }
568 
569  $tag .= "/>";
570 
571  return $tag;
572 
573  }
574 
575 
584  function actions($params, &$smarty){
585  if ( !isset($params['var']) ) throw new Exception('actions: var is a required parameter.', E_USER_ERROR);
586  $varname = $params['var'];
587  unset($params['var']);
588  import( 'Dataface/ActionTool.php');
589  $actionTool =& Dataface_ActionTool::getInstance();
590  if ( !isset($params['record']) ){
591  $params['record'] =& $this->ENV['record'];
592  }
593  $actions = $actionTool->getActions($params);
594  $context = array($varname=>$actions);
595  $smarty->assign($context);
596 
597  }
598 
599 
600  function actions_menu($params, &$smarty){
601 
602  $context = array();
603  if ( isset( $params['id'] ) ) {
604  $context['id'] = $params['id'];
605  unset($params['id']);
606  } else {
607  $context['id'] = '';
608  }
609  if ( isset( $params['class'] ) ) {
610  $context['class'] = $params['class'];
611  unset($params['class']);
612  } else {
613  $context['class'] = '';
614  }
615 
616  if ( isset( $params['id_prefix'] ) ) {
617  $context['id_prefix'] = $params['id_prefix'];
618  unset($params['id_prefix']);
619  } else {
620  $context['id_prefix'] = '';
621  }
622 
623  if ( isset( $params['selected_action'] ) ) {
624  $context['selected_action'] = $params['selected_action'];
625  unset($params['selected_action']);
626  } else {
627  $context['selected_action'] = '';
628  }
629 
630  if ( isset( $params['actions'] ) ){
631  $addon_actions = & $params['actions'];
632  } else {
633  $addon_actions = null;
634  }
635 
636 
637 
638 
639  //$params['var'] = 'actions';
640  //$this->actions($params, $smarty);
641  //print_r($
642  import( 'Dataface/ActionTool.php');
643  $actionTool =& Dataface_ActionTool::getInstance();
644  $actions = $actionTool->getActions($params);
645  if ( $addon_actions !== null ){
646  $p2 = $params;
647  unset($p2['category']);
648  $actions = array_merge($actions, $actionTool->getActions($p2,$addon_actions));
649  usort($actions, array(&$actionTool, '_compareActions'));
650  }
651 
652  foreach ($actions as $k=>$a){
653  if ( @$a['subcategory'] ){
654  $p2 = $params;
655  $p2['category'] = $a['subcategory'];
656  $subactions = $actionTool->getActions($p2);
657 
658  $actions[$k]['subactions'] = $subactions;
659 
660  }
661 
662  }
663  //print_r($actions);
664  $context['actions'] =& $actions;
665  //$smarty->assign($context);
666  if ( isset($params['mincount']) and intval($params['mincount']) > count($context['actions']) ) return;
667  if ( isset($params['maxcount']) and intval($params['maxcount']) < count($context['actions']) ){
668  $more = array(
669  'name'=>'more',
670  'label'=> df_translate('actions_menu.more.label', 'More'),
671  'subactions' => array(),
672  'description' => df_translate('actions_menu.more.description','More actions...'),
673  'url'=>'#'
674 
675 
676  );
677 
678  $existing = array();
679 
680  $i = 0;
681  $lastExistingKey = null;
682  foreach ($actions as $k=>$a){
683  $i++;
684  if ( $i< $params['maxcount'] ){
685  $existing[$k] = $a;
686  $lastExistingKey = $k;
687  } else {
688  $more['subactions'][$k] = $a;
689 
690  }
691  }
692  $existing['more'] = $more;
693  $context['actions'] = $existing;
694  }
695  $smarty->display($context, 'Dataface_ActionsMenu.html');
696 
697  }
698 
699  function record_actions($params, &$smarty){
700  $params['category'] = 'record_actions';
701  return $this->actions_menu($params, $smarty);
702  }
703 
704  function record_tabs($params, &$smarty){
705  $params['category'] = 'record_tabs';
706  if ( is_a($this->ENV['record'], 'Dataface_Record') ){
707  $params['record'] =& $this->ENV['record'];
708  }
709  $table =& Dataface_Table::loadTable($this->ENV['table']);
710  $params2 = array();
711 
712  $params['actions'] = $table->getRelationshipsAsActions($params2);
713  return $this->actions_menu($params, $smarty);
714 
715  }
716 
717 
718  function summary_list($params, &$smarty){
719  import('Dataface/SummaryList.php');
720  $sl = new Dataface_SummaryList($params['records']);
721  return $sl->toHtml();
722  }
723 
724  function glance_list($params, &$smarty){
725  import('Dataface/GlanceList.php');
726  $gl = new Dataface_GlanceList($params['records']);
727  return $gl->toHtml();
728  }
729 
730  function sort_controller($params, &$smarty){
731  import('Dataface/SortControl.php');
732  if ( !isset($params['fields']) ){
733  if ( !isset($params['table']) ) $params['table'] = $this->ENV['QUERY']['-table'];
734  $params['fields'] = $params['table'];
735  }
736 
737  $fields = $params['fields'];
738  if ( isset($params['prefix']) ){
739  $params['prefix'] = null;
740  }
741  $sc = new Dataface_SortControl($fields, $params['prefix']);
742  return $sc->toHtml();
743  }
744 
745 
746  function use_macro($params, $content, &$smarty){
747  if ( isset( $content ) ){
748 
749  $smarty->display($params['file']);
750  $stack =& $smarty->get_template_vars('__macro_stack__');
751  array_pop($stack);
752 
753  } else {
754  $vars =& $smarty->get_template_vars();
755  if ( !isset($vars['__macro_stack__']) || !is_array($vars['__macro_stack__']) ){
756  $stack = array();
757 
758  $vars['__macro_stack__'] =& $stack;
759  }
760  array_push($vars['__macro_stack__'], array());
761 
762  }
763 
764  }
765 
766  function editable($params, $content, &$smarty){
767  if ( isset($content) ){
768  if ( $this->app->_conf['usage_mode'] == 'edit' ){
769  return <<<END
770  <span id="{$params['id']}" class="editable">{$content}</span>
771 END;
772  } else {
773  return $content;
774  }
775 
776  }
777  }
778 
779 
780 
781  function define_slot($params, $content, &$smarty, &$repeat){
782  if ( isset($content) ) {
783  if ( $repeat) echo "We are repeating $params[name]";
784  if ( @$this->app->_conf['debug'] ) $content = '<!-- Begin Slot '.$params['name'].' -->'.$content.'<!-- End Slot '.$params['name'].' -->';
785  return $content;
786  }
787 
788  // From this point on we can assume we're in the first iteration
789  $stack =& $smarty->get_template_vars('__macro_stack__');
790  $local_vars =& $stack[count($stack)-1];
791  foreach ( array_reverse(array_keys($stack) ) as $macroIndex) {
792  $local_vars =& $stack[$macroIndex];
793  if ( isset( $local_vars['__slots__'][$params['name']]) ){
794  // we found a slot to display here.
795  // tell smarty not to execute the inside of this
796  $repeat=false; //
797  echo $local_vars['__slots__'][$params['name']];
798  //display the slot and return
799  return;
800  }
801  unset($local_vars);
802  }
803  if ( isset($params['table']) ) $tname = $params['table'];
804  else $tname = $this->ENV['table'];
805 
807  $out = $table->getBlockContent($params['name']);
808  if ( isset($out) ) {
809  // We found a block to display here.
810  $repeat = false; // tell smarty not to execute inside of tag
811 
812  // Display the block and return
813  echo $out;
814  return;
815  }
816 
817 
818  }
819 
820  function fill_slot($params, $content, &$smarty){
821  if ( isset($content) ){
822  // we are opening the tag
823  $stack =& $smarty->get_template_vars('__macro_stack__');
824  $vars =& $stack[count($stack)-1];
825  $vars['__slots__'][$params['name']] = $content;
826  return '';
827  }
828 
829  }
830 
831  function translate($params, $content, &$smarty){
832  if ( isset($content) ){
833  if ( !isset($params['id']) ) return $content;
834  $id = $params['id'];
835  unset($params['id']);
836  return $this->languageTool->translate($id, $content, $params);
837  }
838  }
839 
840  function result_controller($params,&$smarty){
841 
842  if ( isset($params['table']) ){
843  import('Dataface/ResultController.php');
844  $base_url = ( isset($params['base_url']) ? $params['base_url'] : '');
845  $query = ( isset($params['query']) ? $params['query'] : array('-table'=>$params['table']));
846  $query['-table'] = $params['table'];
847  $controller = new Dataface_ResultController($params['table'], '', $base_url, $query);
848 
849  } else {
850  $controller =& $this->getResultController();
851  }
852  echo $controller->toHtml();
853 
854  }
855 
856 
857  function next_link($params, &$smarty){
858  $controller =& $this->getResultController();
859  echo $controller->getNextLinkHtml(null, @$params['mode']);
860 
861  }
862 
863  function prev_link($params, &$smarty){
864  $controller =& $this->getResultController();
865  echo $controller->getPrevLinkHtml(null, @$params['mode']);
866  }
867 
868  function jump_menu($params,&$smarty){
869  $controller =& $this->getResultController();
870  echo $controller->jumpMenu();
871  }
872 
873  function limit_field($params, &$smarty){
874  $controller =& $this->getResultController();
875  echo $controller->limitField();
876  }
877 
878  function result_index($params, &$smarty){
879  $controller =& $this->getResultController();
880  echo $controller->getPageIndexHtml();
881  }
882  function result_list($params, &$smarty){
883  import( 'Dataface/ResultList.php');
884  $query =& $this->app->getQuery();
885 
886  if ( isset($params['columns']) ){
887  $columns = explode(',',$params['columns']);
888  } else {
889  $columns = array();
890  }
891  $list = new Dataface_ResultList( $query['-table'], $this->app->db(), $columns, $query);
892  echo $list->toHtml();
893 
894  }
895 
896 
897  function filters($params, &$smarty){
898  import( 'Dataface/ResultList.php');
899  $query =& $this->app->getQuery();
900 
901  if ( isset($params['columns']) ){
902  $columns = explode(',',$params['columns']);
903  } else {
904  $columns = array();
905  }
906  $list = new Dataface_ResultList( $query['-table'], $this->app->db(), $columns, $query);
907  echo $list->getResultFilters();
908  }
909 
910  function records($params, &$smarty){
911  $table = null;
912  if ( isset($params['table']) ){
913  $table = $params['table'];
914  unset($params['table']);
915  }
916 
917  if ( isset($params['var']) ){
918  $varname = $params['var'];
919  unset($params['var']);
920  } else {
921  throw new Exception("{records} tag requires the var parameter to be set.");
922  }
923 
924  $q = array_merge($this->app->getQuery(), $params);
925  if ( isset($table) ) $q['-table'] = $table;
926  $vars =& $smarty->get_template_vars();
927  $vars[$varname] = df_get_records_array($q['-table'], $q);
928  }
929 
930  function related_list($params, &$smarty){
931  import('Dataface/RelatedList.php');
932  $query =& $this->app->getQuery();
933  if ( isset($params['record']) ) $record =& $params['record'];
934  else $record =& $this->ENV['resultSet']->loadCurrent();
935 
936  if ( !$record ) {
937  throw new Exception('No record found from which to form related list.', E_USER_ERROR);
938  }
939 
940  if ( isset($params['relationship']) ){
941  $relationship = $params['relationship'];
942  } else if ( isset($query['-relationship']) ){
943  $relationship = $query['-relationship'];
944  } else {
945  throw new Exception('No relationship specified for related list.', E_USER_ERROR);
946  }
947 
948  $relatedList = new Dataface_RelatedList($record, $relationship);
949  echo $relatedList->toHtml();
950  }
951 
952  function bread_crumbs($params, &$smarty){
953  $base = null;
954  if ( $this->app->_query['-mode'] === 'browse' and $this->app->_query['-action'] != 'new'){
955  $record =& $this->app->getRecord();
956  $base = '';
957  if ( $record ){
958  foreach ( $record->getBreadCrumbs() as $label=>$url){
959  $base .= ' :: <a href="'.$url.'" id="bread-crumbs-'.str_replace(' ','_', $label).'">'.$label.'</a>';
960  }
961  }
962  $base = substr($base, 4);
963 
964  }
965 
966  $del = Dataface_Application::getInstance()->getDelegate();
967  if ( !$base and $del and method_exists($del, 'getBreadCrumbs') ){
968  $bc = $del->getBreadCrumbs();
969  if ($bc ){
970  $base = '';
971 
972  foreach ( $bc as $label=>$url){
973  $base .= ' :: <a href="'.$url.'" id="bread-crumbs-'.str_replace(' ','_', $label).'">'.$label.'</a>';
974  }
975  }
976  }
977  if ( !$base ){
978  $table =& Dataface_Table::loadTable($this->ENV['table']);
979  $base = $table->getLabel();
980  }
981 
982 
983 
984  $action =& $this->app->getAction();
985  if ( PEAR::isError($action) ){
986  return '';
987  }
988  $base .= ' :: '.Dataface_LanguageTool::translate(
989  $action['label_i18n'],
990  $action['label']);
991  return "<b>".df_translate('scripts.Dataface_SkinTool.LABEL_BREADCRUMB', "You are here").":</b> ".$base;
992  }
993 
994  function search_form($params, &$smarty){
995  $query =& $this->app->getQuery();
996  $table = isset($params['table']) ? $params['table'] : $query['-table'];
997  $form =& df_create_search_form($table, $query);
998  ob_start();
999  $form->display();
1000  $out = ob_get_contents();
1001  ob_end_clean();
1002  return $out;
1003 
1004  }
1005 
1017  function if_allowed($params, $content, &$smarty){
1018  if ( isset( $content ) ){
1019  if ( !isset( $params['permission'] ) ){
1020  throw new Exception('Missing permission parameter in if_allowed tag.', E_USER_ERROR);
1021  }
1022  if ( isset( $params['record']) ) {
1023  $allowed = $params['record']->checkPermission($params['permission'],$params);
1024  } else if ( isset($params['table']) ){
1025  $table =& Dataface_Table::loadTable($params['table']);
1026 
1027  $perms = $table->getPermissions($params);
1028  $allowed = @$perms[$params['permission']];
1029  } else {
1030  $perms = Dataface_Application::getInstance()->getPermissions();
1031  if ( @$perms[$params['permission']] ) $allowed = true;
1032  else $allowed = false;
1033  }
1034  if ( $allowed ) return $content;
1035  return '';
1036  }
1037  }
1038 
1039  function language_selector($params, &$smarty){
1041  echo $languageTool->getLanguageSelectorHtml($params);
1042  }
1043 
1044  function block($params, &$smarty){
1045  ob_start();
1046  df_block($params);
1047  $out = ob_get_contents();
1048  ob_end_clean();
1049  return $out;
1050 
1051  }
1052 
1053  function feed($params, &$smarty){
1054  if ( isset($params['query']) ) parse_str($params['query'], $query);
1055  else $query = array();
1056  unset($params['query']);
1057 
1058  if ( isset($params['table']) ){
1059  $query['-table'] = $params['table'];
1060  unset($params['table']);
1061  }
1062 
1063  if ( isset($params['relationship']) ){
1064  $query['-relationship'] = $params['relationship'];
1065  unset($params['relationship']);
1066  }
1067 
1068  if ( isset($params['format']) ){
1069  $query['--format'] = $params['format'];
1070  unset($params['format']);
1071  }
1072 
1073  if ( isset($params['url']) ){
1074  $url = $params['url'];
1075  } else {
1076  $url = DATAFACE_SITE_HREF;
1077  }
1078 
1079  if ( isset($params['size']) and $params['size'] == 'large' ){
1080  $icon = 'feed-icon-28x28.png';
1081  } else {
1082  $icon = 'feed-icon-14x14.png';
1083  }
1084 
1085  $query['-action'] = 'feed';
1087  $appq = $app->url($query);
1088  $url = $url .'?'.substr( $appq, strpos($appq,'?')+1);
1089  echo '<a style="display:inline !important" class="feed-link" href="'.htmlspecialchars($url).'" title="Subscribe to feed"><img style="display:inline !important" src="'.DATAFACE_URL.'/images/'.$icon.'" alt="Feed"/></a>';
1090  }
1091 
1092  function abs($params, $url, &$smarty){
1093  return df_absolute_url($url);
1094  }
1095 
1096  function form_context($params, &$smarty){
1097  $query = Dataface_Application::getInstance()->getQuery();
1098  $exclude = array();
1099  if ( @$params['exclude'] ){
1100  $tmp = array_map('trim', explode(',', $params['exclude']));
1101  foreach ($tmp as $t){
1102  $exclude[$t] = $t;
1103  }
1104  }
1105  $fields = array();
1106  foreach ($query as $k=>$v){
1107  if ( isset($exclude[$k]) ) continue;
1108  if ( is_string($v) and strlen($k)>1 and $k{0} === '-' and $k{1} !== '-' ){
1109  $fields[] = '<input type="hidden" name="'.htmlspecialchars($k).'" value="'.htmlspecialchars($v).'"/>';
1110  } else if ( @$params['filters'] and is_string($v) and strlen($v)>0 and strlen($k)>0 and $k{0} !== '-'){
1111  $fields[] = '<input type="hidden" name="'.htmlspecialchars($k).'" value="'.htmlspecialchars($v).'"/>';
1112  }
1113  }
1114 
1115 
1116  return implode("\n", $fields);
1117  }
1118 
1119 
1120 
1121 
1122 
1123 
1124 
1125 }