00001 <?php
00002 define('GYRO_COMMAND_SEP', '_');
00003 define('GYRO_COMMAND_ID_SEP', ',');
00004
00005
00006
00007
00008
00009
00010
00011 class CommandBase implements ICommand {
00012
00013
00014
00015
00016
00017 protected $obj;
00018
00019
00020
00021
00022
00023 protected $params;
00024
00025
00026
00027
00028
00029 protected $result = false;
00030
00031 public function __construct($obj = null, $params = false) {
00032 $this->obj = $obj;
00033 $this->params = $params;
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043 public function can_execute($user) {
00044 $name = $this->get_name();
00045 $ret = true;
00046 if (!empty($name)) {
00047 $ret = AccessControl::is_allowed($name, $this->get_instance(), $this->get_params(), $user);
00048 }
00049 return $ret;
00050 }
00051
00052
00053
00054
00055
00056
00057 public function execute() {
00058 $ret = new Status();
00059 EventSource::Instance()->invoke_event('command_executed', $this, $ret);
00060 return $ret;
00061 }
00062
00063
00064
00065
00066 public function undo() {
00067
00068 }
00069
00070
00071
00072
00073 public function get_name() {
00074 return '';
00075 }
00076
00077
00078
00079
00080 public function get_description() {
00081 return $this->get_name();
00082 }
00083
00084
00085
00086
00087
00088
00089 public function get_instance() {
00090 return $this->obj;
00091 }
00092
00093
00094
00095
00096
00097
00098 public function get_params() {
00099 return $this->params;
00100 }
00101
00102
00103
00104
00105 public function get_success_message() {
00106 return '';
00107 }
00108
00109
00110
00111
00112
00113
00114 public function get_result() {
00115 return $this->result;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 public function serialize() {
00128 $arr = array(
00129 'cmd',
00130 $this->serialize_instance_name($this->get_instance()),
00131 $this->get_name_serialized()
00132 );
00133 return implode(GYRO_COMMAND_SEP, $arr);
00134 }
00135
00136
00137
00138
00139
00140
00141 public function get_name_serialized() {
00142 $arr = array(
00143 $this->get_name()
00144 );
00145 $params = $this->serialize_params($this->get_params());
00146 if ($params) {
00147 $arr[] = $params;
00148 }
00149 return implode(GYRO_COMMAND_SEP, $arr);
00150 }
00151
00152
00153 protected function serialize_instance_name($inst) {
00154 $ret = '';
00155 if ($inst instanceof IDataObject) {
00156 $arr = array(
00157 $inst->get_table_name()
00158 );
00159 $arr_id = array();
00160 foreach ($inst->get_table_keys() as $key => $field) {
00161 $arr_id[] = $inst->$key;
00162 }
00163 $arr[] = implode(GYRO_COMMAND_ID_SEP, $arr_id);
00164 $ret = implode(GYRO_COMMAND_SEP, $arr);
00165 }
00166 else {
00167 $ret = Cast::string($inst);
00168 }
00169 return $ret;
00170 }
00171
00172 protected function serialize_params($params) {
00173 $ret = '';
00174 if (is_array($params)) {
00175 $arr = array();
00176 foreach($params as $key => $value) {
00177 $arr[] = $key;
00178 $arr[] = $value;
00179 }
00180 $ret = implode(GYRO_COMMAND_SEP, $arr);
00181 }
00182 else {
00183 $ret = Cast::string($params);
00184 }
00185 return $ret;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 protected function set_result($result) {
00198 $this->result = $result;
00199 }
00200
00201
00202
00203
00204
00205
00206 protected function illegal_data_error() {
00207 return new Status(tr('Illegal Command Data for Command %cmd%', 'core', array('%cmd%' => get_class($this))));
00208 }
00209 }