00001 <?php
00002
00003
00004
00005
00006
00007
00008 class WidgetItemMenu implements IWidget {
00009 const SORTED = 128;
00010 const SEPARATE_COMMANDS = 256;
00011
00012 public $item;
00013 public $context;
00014 public $params;
00015 public $aro;
00016 public $args;
00017
00018 public static function output($item, $context = 'view', $params = false, $args = array(), $policy = self::NONE) {
00019 $w = new WidgetItemMenu($item, $context, $params, $args);
00020 return $w->render($policy);
00021 }
00022
00023 public function __construct($item, $context = 'view', $params = false, $args = array()) {
00024 $this->item = $item;
00025 $this->context = $context;
00026 $this->params = $params;
00027 $this->aro = AccessControl::get_current_aro();
00028 $this->args = $args;
00029 }
00030
00031 public function render($policy = self::NONE) {
00032 $ret = '';
00033 $sources = $this->retrieve_actions($this->item);
00034 $commands = array();
00035 $actions = array();
00036 $has_commands = false;
00037
00038 if (Common::flag_is_set($policy, self::SEPARATE_COMMANDS)) {
00039 $this->separate($sources, $actions, $commands);
00040 $has_commands = (count($commands) > 0);
00041 }
00042 else {
00043 $actions = $sources;
00044 foreach($sources as $s) {
00045 if ($s instanceof ICommand) {
00046 $has_commands = true;
00047 break;
00048 }
00049 }
00050 }
00051
00052 if (Common::flag_is_set($policy, self::SORTED)) {
00053 $this->sort($actions);
00054 $this->sort($commands);
00055 }
00056
00057 if ($has_commands || count($actions)) {
00058 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'core::widgets/menu');
00059 $view->assign('actions', $actions);
00060
00061 if ($has_commands) {
00062 Load::tools('formhandler');
00063 $formhandler = new FormHandler('process_commands', 'process_commands', FormHandler::TOKEN_POLICY_REUSE);
00064 $formhandler->prepare_view($view);
00065 }
00066
00067 $view->assign('commands', $commands);
00068 $view->assign('policy', $policy);
00069 $view->assign('css_class', Arr::get_item($this->args, 'css_class', 'list_menu'));
00070 $ret = $view->render();
00071 }
00072
00073 return $ret;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 protected function retrieve_actions($item) {
00083 $actions = array();
00084 if ($item instanceof IActionSource) {
00085 $actions = $this->item->get_actions($this->aro, $this->context, $this->params);
00086 }
00087 else if (is_array($item)) {
00088 $actions = $this->item;
00089 }
00090 return $actions;
00091 }
00092
00093
00094
00095
00096 protected function separate($arr_in, &$out_actions, &$out_commands) {
00097 foreach($arr_in as $action) {
00098
00099 if ($action instanceof ICommand) {
00100 $out_commands[] = $action;
00101 }
00102 else if ($action instanceof IAction) {
00103 $out_actions[] = $action;
00104 }
00105 }
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 protected function sort_compare_callback($a, $b) {
00116 $da = $a->get_description();
00117 $db = $b->get_description();
00118 if ($da == $db) {
00119 return 0;
00120 }
00121 return ($da < $db) ? -1 : 1;
00122 }
00123
00124
00125
00126
00127 protected function sort(&$arr_actions) {
00128 usort($arr_actions, array($this, 'sort_compare_callback'));
00129 }
00130 }