00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 class WidgetBreadcrumb implements IWidget {
00021
00022
00023
00024 const LINK_LAST = 128;
00025 const USE_PREFIX = 256;
00026 const BLOCK = 512;
00027
00028 const UNLINK_LAST = 1024;
00029
00030 public $source;
00031 public $action = 'view';
00032 public $action_params = null;
00033 public $prefix = '> ';
00034 public $glue = ' > ';
00035 public $url_home;
00036
00037 public static function output($source, $policy = self::NONE) {
00038 $w = new WidgetBreadcrumb($source);
00039 return $w->render($policy);
00040 }
00041
00042 public function __construct($source) {
00043 $this->url_home = Config::get_value(Config::URL_BASEDIR);
00044 $this->source = $source;
00045 }
00046
00047 public function render($policy = self::NONE) {
00048 $src = $this->to_array($this->source);
00049 $crumb = array();
00050 $unlink_this = !Common::flag_is_set($policy, self::LINK_LAST);
00051 foreach($src as $item) {
00052 $link = ($unlink_this) ? preg_replace('|<a.*?>(.*?)</a>|', '$1', $item) : $item;
00053 array_unshift($crumb, $link);
00054 $unlink_this = false;
00055 }
00056 $home = html::a(Config::get_value(Config::TITLE), $this->url_home, tr('Go to home page', 'app'));
00057 array_unshift($crumb, $home);
00058
00059 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'core::widgets/breadcrumb');
00060 $view->assign('breadcrumb_prefix', Common::flag_is_set($policy, self::USE_PREFIX) ? $this->prefix : '');
00061 $view->assign('breadcrumb_glue', $this->glue);
00062 $view->assign('breadcrumb_items', $crumb);
00063
00064 return $view->render();
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 protected function to_array($item, $key = false) {
00074 $arr_ret = array();
00075 if ($item instanceof IHierarchic) {
00076 $arr_ret[] = $this->instance2string($item, $key);
00077 $arr_ret = array_merge($arr_ret, $this->to_array($item->get_parent(), $key));
00078 }
00079 else if ($item instanceof ISelfDescribing) {
00080 $arr_ret[] = $this->instance2string($item, $key);
00081 }
00082 else if (is_array($item)) {
00083 foreach($item as $subkey => $subitem) {
00084 $arr_ret = array_merge($this->to_array($subitem, $subkey), $arr_ret);
00085 }
00086 }
00087 else if (!empty($item)) {
00088 $arr_ret[] = @strval($item);
00089 }
00090 return $arr_ret;
00091 }
00092
00093
00094
00095
00096 protected function instance2string($instance, $action) {
00097 if (empty($action) || is_numeric($action)) {
00098 $action = $this->action;
00099 }
00100 return WidgetActionLink::output($instance, $action, $instance);
00101 }
00102 }