| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
abstract class sfAdminGenerator extends sfCrudGenerator |
|---|
| 27 |
{ |
|---|
| 28 |
protected |
|---|
| 29 |
$formObject = null, |
|---|
| 30 |
$fields = array(); |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
* Returns HTML code for a help icon. |
|---|
| 34 |
* |
|---|
| 35 |
* @param string $column The column name |
|---|
| 36 |
* @param string $type The field type (list, edit) |
|---|
| 37 |
* |
|---|
| 38 |
* @return string HTML code |
|---|
| 39 |
*/ |
|---|
| 40 |
public function getHelpAsIcon($column, $type = '') |
|---|
| 41 |
{ |
|---|
| 42 |
$help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help'); |
|---|
| 43 |
if ($help) |
|---|
| 44 |
{ |
|---|
| 45 |
return "[?php echo image_tag(sfConfig::get('sf_admin_web_dir').'/images/help.png', array('align' => 'absmiddle', 'alt' => __('".$this->escapeString($help)."'), 'title' => __('".$this->escapeString($help)."'))) ?]"; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
return ''; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
* Returns HTML code for a help text. |
|---|
| 53 |
* |
|---|
| 54 |
* @param string $column The column name |
|---|
| 55 |
* @param string $type The field type (list, edit) |
|---|
| 56 |
* |
|---|
| 57 |
* @return string HTML code |
|---|
| 58 |
*/ |
|---|
| 59 |
public function getHelp($column, $type = '') |
|---|
| 60 |
{ |
|---|
| 61 |
$help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help'); |
|---|
| 62 |
if ($help) |
|---|
| 63 |
{ |
|---|
| 64 |
return "<div class=\"sf_admin_edit_help\">[?php echo __('".$this->escapeString($help)."') ?]</div>"; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
return ''; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* Returns HTML code for an action button. |
|---|
| 72 |
* |
|---|
| 73 |
* @param string $actionName The action name |
|---|
| 74 |
* @param array $params The parameters |
|---|
| 75 |
* @param boolean $pk_link Whether to add a primary key link or not |
|---|
| 76 |
* |
|---|
| 77 |
* @return string HTML code |
|---|
| 78 |
*/ |
|---|
| 79 |
public function getButtonToAction($actionName, $params, $pk_link = false) |
|---|
| 80 |
{ |
|---|
| 81 |
$params = (array) $params; |
|---|
| 82 |
$options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array(); |
|---|
| 83 |
$method = 'button_to'; |
|---|
| 84 |
$li_class = ''; |
|---|
| 85 |
$only_for = isset($params['only_for']) ? $params['only_for'] : null; |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
if ($actionName[0] == '_') |
|---|
| 89 |
{ |
|---|
| 90 |
$actionName = substr($actionName, 1); |
|---|
| 91 |
$default_name = strtr($actionName, '_', ' '); |
|---|
| 92 |
$default_icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png'; |
|---|
| 93 |
$default_action = $actionName; |
|---|
| 94 |
$default_class = 'sf_admin_action_'.$actionName; |
|---|
| 95 |
|
|---|
| 96 |
if ($actionName == 'save' || $actionName == 'save_and_add' || $actionName == 'save_and_list') |
|---|
| 97 |
{ |
|---|
| 98 |
$method = 'submit_tag'; |
|---|
| 99 |
$options['name'] = $actionName; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
if ($actionName == 'delete') |
|---|
| 103 |
{ |
|---|
| 104 |
$options['post'] = true; |
|---|
| 105 |
if (!isset($options['confirm'])) |
|---|
| 106 |
{ |
|---|
| 107 |
$options['confirm'] = 'Are you sure?'; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
$li_class = 'float-left'; |
|---|
| 111 |
|
|---|
| 112 |
$only_for = 'edit'; |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
else |
|---|
| 116 |
{ |
|---|
| 117 |
$default_name = strtr($actionName, '_', ' '); |
|---|
| 118 |
$default_icon = sfConfig::get('sf_admin_web_dir').'/images/default_icon.png'; |
|---|
| 119 |
$default_action = 'List'.sfInflector::camelize($actionName); |
|---|
| 120 |
$default_class = ''; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
$name = isset($params['name']) ? $params['name'] : $default_name; |
|---|
| 124 |
$icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : $default_icon; |
|---|
| 125 |
$action = isset($params['action']) ? $params['action'] : $default_action; |
|---|
| 126 |
$url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\''; |
|---|
| 127 |
|
|---|
| 128 |
if (!isset($options['class'])) |
|---|
| 129 |
{ |
|---|
| 130 |
if ($default_class) |
|---|
| 131 |
{ |
|---|
| 132 |
$options['class'] = $default_class; |
|---|
| 133 |
} |
|---|
| 134 |
else |
|---|
| 135 |
{ |
|---|
| 136 |
$options['style'] = 'background: #ffc url('.$icon.') no-repeat 3px 2px'; |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
$li_class = $li_class ? ' class="'.$li_class.'"' : ''; |
|---|
| 141 |
|
|---|
| 142 |
$html = '<li'.$li_class.'>'; |
|---|
| 143 |
|
|---|
| 144 |
if ($only_for == 'edit') |
|---|
| 145 |
{ |
|---|
| 146 |
$html .= '[?php if ('.$this->getPrimaryKeyIsSet().'): ?]'."\n"; |
|---|
| 147 |
} |
|---|
| 148 |
else if ($only_for == 'create') |
|---|
| 149 |
{ |
|---|
| 150 |
$html .= '[?php if (!'.$this->getPrimaryKeyIsSet().'): ?]'."\n"; |
|---|
| 151 |
} |
|---|
| 152 |
else if ($only_for !== null) |
|---|
| 153 |
{ |
|---|
| 154 |
throw new sfConfigurationException(sprintf('The "only_for" parameter can only takes "create" or "edit" as argument ("%s").', $only_for)); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
if ($method == 'submit_tag') |
|---|
| 158 |
{ |
|---|
| 159 |
$html .= '[?php echo submit_tag(__(\''.$name.'\'), '.var_export($options, true).') ?]'; |
|---|
| 160 |
} |
|---|
| 161 |
else |
|---|
| 162 |
{ |
|---|
| 163 |
$phpOptions = var_export($options, true); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
$phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions); |
|---|
| 167 |
|
|---|
| 168 |
$html .= '[?php echo button_to(__(\''.$name.'\'), \''.$this->getModuleName().'/'.$action.$url_params.', '.$phpOptions.') ?]'; |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
if ($only_for !== null) |
|---|
| 172 |
{ |
|---|
| 173 |
$html .= '[?php endif; ?]'."\n"; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
$html .= '</li>'."\n"; |
|---|
| 177 |
|
|---|
| 178 |
return $html; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* Returns HTML code for an action link. |
|---|
| 183 |
* |
|---|
| 184 |
* @param string $actionName The action name |
|---|
| 185 |
* @param array $params The parameters |
|---|
| 186 |
* @param boolean $pk_link Whether to add a primary key link or not |
|---|
| 187 |
* |
|---|
| 188 |
* @return string HTML code |
|---|
| 189 |
*/ |
|---|
| 190 |
public function getLinkToAction($actionName, $params, $pk_link = false) |
|---|
| 191 |
{ |
|---|
| 192 |
$options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array(); |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
if ($actionName[0] == '_') |
|---|
| 196 |
{ |
|---|
| 197 |
$actionName = substr($actionName, 1); |
|---|
| 198 |
$name = $actionName; |
|---|
| 199 |
$icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png'; |
|---|
| 200 |
$action = $actionName; |
|---|
| 201 |
|
|---|
| 202 |
if ($actionName == 'delete') |
|---|
| 203 |
{ |
|---|
| 204 |
$options['post'] = true; |
|---|
| 205 |
if (!isset($options['confirm'])) |
|---|
| 206 |
{ |
|---|
| 207 |
$options['confirm'] = 'Are you sure?'; |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
else |
|---|
| 212 |
{ |
|---|
| 213 |
$name = isset($params['name']) ? $params['name'] : $actionName; |
|---|
| 214 |
$icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : sfConfig::get('sf_admin_web_dir').'/images/default_icon.png'; |
|---|
| 215 |
$action = isset($params['action']) ? $params['action'] : 'List'.sfInflector::camelize($actionName); |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
$url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\''; |
|---|
| 219 |
|
|---|
| 220 |
$phpOptions = var_export($options, true); |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
$phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions); |
|---|
| 224 |
|
|---|
| 225 |
return '<li>[?php echo link_to(image_tag(\''.$icon.'\', array(\'alt\' => __(\''.$name.'\'), \'title\' => __(\''.$name.'\'))), \''.$this->getModuleName().'/'.$action.$url_params.($options ? ', '.$phpOptions : '').') ?]</li>'."\n"; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
* Returns HTML code for an action option in a select tag. |
|---|
| 230 |
* |
|---|
| 231 |
* @param string $actionName The action name |
|---|
| 232 |
* @param array $params The parameters |
|---|
| 233 |
* |
|---|
| 234 |
* @return string HTML code |
|---|
| 235 |
*/ |
|---|
| 236 |
public function getOptionToAction($actionName, $params) |
|---|
| 237 |
{ |
|---|
| 238 |
$options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array(); |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
if ($actionName[0] == '_') |
|---|
| 242 |
{ |
|---|
| 243 |
$actionName = substr($actionName, 1); |
|---|
| 244 |
if ($actionName == 'deleteSelected') |
|---|
| 245 |
{ |
|---|
| 246 |
$params['name'] = 'Delete Selected'; |
|---|
| 247 |
} |
|---|
| 248 |
} |
|---|
| 249 |
$name = isset($params['name']) ? $params['name'] : $actionName; |
|---|
| 250 |
|
|---|
| 251 |
$options['value'] = $actionName; |
|---|
| 252 |
|
|---|
| 253 |
$phpOptions = var_export($options, true); |
|---|
| 254 |
|
|---|
| 255 |
return '[?php echo content_tag(\'option\', __(\''.$name.'\')'.($options ? ', '.$phpOptions : '').') ?]'; |
|---|
| 256 |
} |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
* Returns HTML code for a column in edit mode. |
|---|
| 260 |
* |
|---|
| 261 |
* @param string $column The column name |
|---|
| 262 |
* @param array $params The parameters |
|---|
| 263 |
* |
|---|
| 264 |
* @return string HTML code |
|---|
| 265 |
*/ |
|---|
| 266 |
public function getColumnEditTag($column, $params = array()) |
|---|
| 267 |
{ |
|---|
| 268 |
|
|---|
| 269 |
$user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params'); |
|---|
| 270 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 271 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 272 |
|
|---|
| 273 |
if ($column->isComponent()) |
|---|
| 274 |
{ |
|---|
| 275 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 276 |
} |
|---|
| 277 |
else if ($column->isPartial()) |
|---|
| 278 |
{ |
|---|
| 279 |
return "get_partial('".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
$params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params); |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
$type = $column->getType(); |
|---|
| 287 |
if ($type == PropelColumnTypes::DATE) |
|---|
| 288 |
{ |
|---|
| 289 |
$params = array_merge(array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params); |
|---|
| 290 |
} |
|---|
| 291 |
else if ($type == PropelColumnTypes::TIMESTAMP) |
|---|
| 292 |
{ |
|---|
| 293 |
$params = array_merge(array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params); |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
if ($inputType = $this->getParameterValue('edit.fields.'.$column->getName().'.type')) |
|---|
| 298 |
{ |
|---|
| 299 |
if ($inputType == 'plain') |
|---|
| 300 |
{ |
|---|
| 301 |
return $this->getColumnListTag($column, $params); |
|---|
| 302 |
} |
|---|
| 303 |
else |
|---|
| 304 |
{ |
|---|
| 305 |
return $this->getPHPObjectHelper($inputType, $column, $params); |
|---|
| 306 |
} |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
return parent::getCrudColumnEditTag($column, $params); |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
* Returns all column categories. |
|---|
| 315 |
* |
|---|
| 316 |
* @param string $paramName The parameter name |
|---|
| 317 |
* |
|---|
| 318 |
* @return array The column categories |
|---|
| 319 |
*/ |
|---|
| 320 |
public function getColumnCategories($paramName) |
|---|
| 321 |
{ |
|---|
| 322 |
if (is_array($this->getParameterValue($paramName))) |
|---|
| 323 |
{ |
|---|
| 324 |
$fields = $this->getParameterValue($paramName); |
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
if (!isset($fields[0])) |
|---|
| 328 |
{ |
|---|
| 329 |
return array_keys($fields); |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
return array('NONE'); |
|---|
| 335 |
} |
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
* Wraps content with a credential condition. |
|---|
| 339 |
* |
|---|
| 340 |
* @param string $content The content |
|---|
| 341 |
* @param array $params The parameters |
|---|
| 342 |
* |
|---|
| 343 |
* @return string HTML code |
|---|
| 344 |
*/ |
|---|
| 345 |
public function addCredentialCondition($content, $params = array()) |
|---|
| 346 |
{ |
|---|
| 347 |
if (isset($params['credentials'])) |
|---|
| 348 |
{ |
|---|
| 349 |
$credentials = str_replace("\n", ' ', var_export($params['credentials'], true)); |
|---|
| 350 |
|
|---|
| 351 |
return <<<EOF |
|---|
| 352 |
[?php if (\$sf_user->hasCredential($credentials)): ?] |
|---|
| 353 |
$content |
|---|
| 354 |
[?php endif; ?] |
|---|
| 355 |
EOF; |
|---|
| 356 |
} |
|---|
| 357 |
else |
|---|
| 358 |
{ |
|---|
| 359 |
return $content; |
|---|
| 360 |
} |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
* Gets sfAdminColumn objects for a given category. |
|---|
| 365 |
* |
|---|
| 366 |
* @param string $paramName The parameter name |
|---|
| 367 |
* @param string $category The category |
|---|
| 368 |
* |
|---|
| 369 |
* @return array sfAdminColumn array |
|---|
| 370 |
*/ |
|---|
| 371 |
public function getColumns($paramName, $category = 'NONE') |
|---|
| 372 |
{ |
|---|
| 373 |
$phpNames = array(); |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
$fields = $this->getParameterValue($paramName); |
|---|
| 377 |
if (is_array($fields)) |
|---|
| 378 |
{ |
|---|
| 379 |
|
|---|
| 380 |
if (isset($fields[0])) |
|---|
| 381 |
{ |
|---|
| 382 |
|
|---|
| 383 |
$fields = array('NONE' => $fields); |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
if (!$fields) |
|---|
| 387 |
{ |
|---|
| 388 |
return array(); |
|---|
| 389 |
} |
|---|
| 390 |
|
|---|
| 391 |
foreach ($fields[$category] as $field) |
|---|
| 392 |
{ |
|---|
| 393 |
list($field, $flags) = $this->splitFlag($field); |
|---|
| 394 |
|
|---|
| 395 |
$phpNames[] = $this->getAdminColumnForField($field, $flags); |
|---|
| 396 |
} |
|---|
| 397 |
} |
|---|
| 398 |
else |
|---|
| 399 |
{ |
|---|
| 400 |
|
|---|
| 401 |
return $this->getAllColumns(); |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
return $phpNames; |
|---|
| 405 |
} |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
* Gets modifier flags from a column name. |
|---|
| 409 |
* |
|---|
| 410 |
* @param string $text The column name |
|---|
| 411 |
* |
|---|
| 412 |
* @return array An array of detected flags |
|---|
| 413 |
*/ |
|---|
| 414 |
public function splitFlag($text) |
|---|
| 415 |
{ |
|---|
| 416 |
$flags = array(); |
|---|
| 417 |
while (in_array($text[0], array('=', '-', '+', '_', '~'))) |
|---|
| 418 |
{ |
|---|
| 419 |
$flags[] = $text[0]; |
|---|
| 420 |
$text = substr($text, 1); |
|---|
| 421 |
} |
|---|
| 422 |
|
|---|
| 423 |
return array($text, $flags); |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
* Gets a parameter value. |
|---|
| 428 |
* |
|---|
| 429 |
* @param string $key The key name |
|---|
| 430 |
* @param mixed $default The default value |
|---|
| 431 |
* |
|---|
| 432 |
* @return mixed The parameter value |
|---|
| 433 |
*/ |
|---|
| 434 |
public function getParameterValue($key, $default = null) |
|---|
| 435 |
{ |
|---|
| 436 |
if (preg_match('/^([^\.]+)\.fields\.(.+)$/', $key, $matches)) |
|---|
| 437 |
{ |
|---|
| 438 |
return $this->getFieldParameterValue($matches[2], $matches[1], $default); |
|---|
| 439 |
} |
|---|
| 440 |
else |
|---|
| 441 |
{ |
|---|
| 442 |
return $this->getValueFromKey($key, $default); |
|---|
| 443 |
} |
|---|
| 444 |
} |
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
* Gets a field parameter value. |
|---|
| 448 |
* |
|---|
| 449 |
* @param string $key The key name |
|---|
| 450 |
* @param string $type The type (list, edit) |
|---|
| 451 |
* @param mixed $default The default value |
|---|
| 452 |
* |
|---|
| 453 |
* @return mixed The parameter value |
|---|
| 454 |
*/ |
|---|
| 455 |
protected function getFieldParameterValue($key, $type = '', $default = null) |
|---|
| 456 |
{ |
|---|
| 457 |
$retval = $this->getValueFromKey($type.'.fields.'.$key, $default); |
|---|
| 458 |
if ($retval !== null) |
|---|
| 459 |
{ |
|---|
| 460 |
return $retval; |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
$retval = $this->getValueFromKey('fields.'.$key, $default); |
|---|
| 464 |
if ($retval !== null) |
|---|
| 465 |
{ |
|---|
| 466 |
return $retval; |
|---|
| 467 |
} |
|---|
| 468 |
|
|---|
| 469 |
if (preg_match('/\.name$/', $key)) |
|---|
| 470 |
{ |
|---|
| 471 |
|
|---|
| 472 |
return sfInflector::humanize(($pos = strpos($key, '.')) ? substr($key, 0, $pos) : $key); |
|---|
| 473 |
} |
|---|
| 474 |
else |
|---|
| 475 |
{ |
|---|
| 476 |
return null; |
|---|
| 477 |
} |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
* Gets the value for a given key. |
|---|
| 482 |
* |
|---|
| 483 |
* @param string $key The key name |
|---|
| 484 |
* @param mixed $default The default value |
|---|
| 485 |
* |
|---|
| 486 |
* @return mixed The key value |
|---|
| 487 |
*/ |
|---|
| 488 |
protected function getValueFromKey($key, $default = null) |
|---|
| 489 |
{ |
|---|
| 490 |
$ref =& $this->params; |
|---|
| 491 |
$parts = explode('.', $key); |
|---|
| 492 |
$count = count($parts); |
|---|
| 493 |
for ($i = 0; $i < $count; $i++) |
|---|
| 494 |
{ |
|---|
| 495 |
$partKey = $parts[$i]; |
|---|
| 496 |
if (!isset($ref[$partKey])) |
|---|
| 497 |
{ |
|---|
| 498 |
return $default; |
|---|
| 499 |
} |
|---|
| 500 |
|
|---|
| 501 |
if ($count == $i + 1) |
|---|
| 502 |
{ |
|---|
| 503 |
return $ref[$partKey]; |
|---|
| 504 |
} |
|---|
| 505 |
else |
|---|
| 506 |
{ |
|---|
| 507 |
$ref =& $ref[$partKey]; |
|---|
| 508 |
} |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
return $default; |
|---|
| 512 |
} |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
* Wraps a content for I18N. |
|---|
| 516 |
* |
|---|
| 517 |
* @param string $key The key name |
|---|
| 518 |
* @param string $default The defaul value |
|---|
| 519 |
* @param bool $withEcho If true, string is wrapped in php echo |
|---|
| 520 |
* |
|---|
| 521 |
* @return string HTML code |
|---|
| 522 |
*/ |
|---|
| 523 |
public function getI18NString($key, $default = null, $withEcho = true) |
|---|
| 524 |
{ |
|---|
| 525 |
$value = $this->escapeString($this->getParameterValue($key, $default)); |
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER); |
|---|
| 529 |
$this->params['tmp']['display'] = array(); |
|---|
| 530 |
foreach ($matches[1] as $name) |
|---|
| 531 |
{ |
|---|
| 532 |
$this->params['tmp']['display'][] = $name; |
|---|
| 533 |
} |
|---|
| 534 |
|
|---|
| 535 |
$vars = array(); |
|---|
| 536 |
foreach ($this->getColumns('tmp.display') as $column) |
|---|
| 537 |
{ |
|---|
| 538 |
if ($column->isLink()) |
|---|
| 539 |
{ |
|---|
| 540 |
$vars[] = '\'%%'.$column->getName().'%%\' => link_to('.$this->getColumnListTag($column).', \''.$this->getModuleName().'/edit?'.$this->getPrimaryKeyUrlParams().')'; |
|---|
| 541 |
} |
|---|
| 542 |
elseif ($column->isPartial()) |
|---|
| 543 |
{ |
|---|
| 544 |
$vars[] = '\'%%_'.$column->getName().'%%\' => '.$this->getColumnListTag($column); |
|---|
| 545 |
} |
|---|
| 546 |
else if ($column->isComponent()) |
|---|
| 547 |
{ |
|---|
| 548 |
$vars[] = '\'%%~'.$column->getName().'%%\' => '.$this->getColumnListTag($column); |
|---|
| 549 |
} |
|---|
| 550 |
else |
|---|
| 551 |
{ |
|---|
| 552 |
$vars[] = '\'%%'.$column->getName().'%%\' => '.$this->getColumnListTag($column); |
|---|
| 553 |
} |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
$value = preg_replace('/%%=([^%]+)%%/', '%%$1%%', $value); |
|---|
| 558 |
|
|---|
| 559 |
$i18n = '__(\''.$value.'\', '."\n".'array('.implode(",\n", $vars).'))'; |
|---|
| 560 |
|
|---|
| 561 |
return $withEcho ? '[?php echo '.$i18n.' ?]' : $i18n; |
|---|
| 562 |
} |
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
* Replaces constants in a string. |
|---|
| 566 |
* |
|---|
| 567 |
* @param string $value |
|---|
| 568 |
* |
|---|
| 569 |
* @return string |
|---|
| 570 |
*/ |
|---|
| 571 |
public function replaceConstants($value) |
|---|
| 572 |
{ |
|---|
| 573 |
|
|---|
| 574 |
preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER); |
|---|
| 575 |
$this->params['tmp']['display'] = array(); |
|---|
| 576 |
foreach ($matches[1] as $name) |
|---|
| 577 |
{ |
|---|
| 578 |
$this->params['tmp']['display'][] = $name; |
|---|
| 579 |
} |
|---|
| 580 |
|
|---|
| 581 |
foreach ($this->getColumns('tmp.display') as $column) |
|---|
| 582 |
{ |
|---|
| 583 |
$value = str_replace('%%'.$column->getName().'%%', '{'.$this->getColumnGetter($column, true, 'this->').'}', $value); |
|---|
| 584 |
} |
|---|
| 585 |
|
|---|
| 586 |
return $value; |
|---|
| 587 |
} |
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
* Returns HTML code for a column in list mode. |
|---|
| 591 |
* |
|---|
| 592 |
* @param string $column The column name |
|---|
| 593 |
* @param array $params The parameters |
|---|
| 594 |
* |
|---|
| 595 |
* @return string HTML code |
|---|
| 596 |
*/ |
|---|
| 597 |
public function getColumnListTag($column, $params = array()) |
|---|
| 598 |
{ |
|---|
| 599 |
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params'); |
|---|
| 600 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 601 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 602 |
|
|---|
| 603 |
$type = $column->getType(); |
|---|
| 604 |
|
|---|
| 605 |
$columnGetter = $this->getColumnGetter($column, true); |
|---|
| 606 |
|
|---|
| 607 |
if ($column->isComponent()) |
|---|
| 608 |
{ |
|---|
| 609 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 610 |
} |
|---|
| 611 |
else if ($column->isPartial()) |
|---|
| 612 |
{ |
|---|
| 613 |
return "get_partial('".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 614 |
} |
|---|
| 615 |
else if ($type == PropelColumnTypes::DATE || $type == PropelColumnTypes::TIMESTAMP) |
|---|
| 616 |
{ |
|---|
| 617 |
$format = isset($params['date_format']) ? $params['date_format'] : ($type == PropelColumnTypes::DATE ? 'D' : 'f'); |
|---|
| 618 |
return "($columnGetter !== null && $columnGetter !== '') ? format_date($columnGetter, \"$format\") : ''"; |
|---|
| 619 |
} |
|---|
| 620 |
elseif ($type == PropelColumnTypes::BOOLEAN) |
|---|
| 621 |
{ |
|---|
| 622 |
return "$columnGetter ? image_tag(sfConfig::get('sf_admin_web_dir').'/images/tick.png') : ' '"; |
|---|
| 623 |
} |
|---|
| 624 |
else |
|---|
| 625 |
{ |
|---|
| 626 |
return "$columnGetter"; |
|---|
| 627 |
} |
|---|
| 628 |
} |
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
* Returns HTML code for a column in filter mode. |
|---|
| 632 |
* |
|---|
| 633 |
* @param string $column The column name |
|---|
| 634 |
* @param array $params The parameters |
|---|
| 635 |
* |
|---|
| 636 |
* @return string HTML code |
|---|
| 637 |
*/ |
|---|
| 638 |
public function getColumnFilterTag($column, $params = array()) |
|---|
| 639 |
{ |
|---|
| 640 |
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params'); |
|---|
| 641 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 642 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 643 |
|
|---|
| 644 |
if ($column->isComponent()) |
|---|
| 645 |
{ |
|---|
| 646 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'filter'))"; |
|---|
| 647 |
} |
|---|
| 648 |
else if ($column->isPartial()) |
|---|
| 649 |
{ |
|---|
| 650 |
return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))"; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
$type = $column->getType(); |
|---|
| 654 |
|
|---|
| 655 |
$default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null"; |
|---|
| 656 |
$unquotedName = 'filters['.$column->getName().']'; |
|---|
| 657 |
$name = "'$unquotedName'"; |
|---|
| 658 |
|
|---|
| 659 |
if ($column->isForeignKey()) |
|---|
| 660 |
{ |
|---|
| 661 |
$params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName)); |
|---|
| 662 |
return "object_select_tag($default_value, null, $params)"; |
|---|
| 663 |
|
|---|
| 664 |
} |
|---|
| 665 |
else if ($type == PropelColumnTypes::DATE) |
|---|
| 666 |
{ |
|---|
| 667 |
|
|---|
| 668 |
$params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png')); |
|---|
| 669 |
return "input_date_range_tag($name, $default_value, $params)"; |
|---|
| 670 |
} |
|---|
| 671 |
else if ($type == PropelColumnTypes::TIMESTAMP) |
|---|
| 672 |
{ |
|---|
| 673 |
|
|---|
| 674 |
$params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png')); |
|---|
| 675 |
return "input_date_range_tag($name, $default_value, $params)"; |
|---|
| 676 |
} |
|---|
| 677 |
else if ($type == PropelColumnTypes::BOOLEAN) |
|---|
| 678 |
{ |
|---|
| 679 |
$defaultIncludeCustom = '__("yes or no")'; |
|---|
| 680 |
|
|---|
| 681 |
$option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom)); |
|---|
| 682 |
$params = $this->getObjectTagParams($params); |
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
$option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params); |
|---|
| 686 |
|
|---|
| 687 |
$options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)"; |
|---|
| 688 |
|
|---|
| 689 |
return "select_tag($name, $options, $params)"; |
|---|
| 690 |
} |
|---|
| 691 |
else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR) |
|---|
| 692 |
{ |
|---|
| 693 |
$size = ($column->getSize() < 15 ? $column->getSize() : 15); |
|---|
| 694 |
$params = $this->getObjectTagParams($params, array('size' => $size)); |
|---|
| 695 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 696 |
} |
|---|
| 697 |
else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT) |
|---|
| 698 |
{ |
|---|
| 699 |
$params = $this->getObjectTagParams($params, array('size' => 7)); |
|---|
| 700 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 701 |
} |
|---|
| 702 |
else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL) |
|---|
| 703 |
{ |
|---|
| 704 |
$params = $this->getObjectTagParams($params, array('size' => 7)); |
|---|
| 705 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 706 |
} |
|---|
| 707 |
else |
|---|
| 708 |
{ |
|---|
| 709 |
$params = $this->getObjectTagParams($params, array('disabled' => true)); |
|---|
| 710 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 711 |
} |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
* Gets the form object |
|---|
| 716 |
* |
|---|
| 717 |
* @return sfForm |
|---|
| 718 |
*/ |
|---|
| 719 |
public function getFormObject() |
|---|
| 720 |
{ |
|---|
| 721 |
if (null === $this->formObject) |
|---|
| 722 |
{ |
|---|
| 723 |
$class = $this->getFormClassName(); |
|---|
| 724 |
|
|---|
| 725 |
$this->formObject = new $class(); |
|---|
| 726 |
} |
|---|
| 727 |
|
|---|
| 728 |
return $this->formObject; |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
* Gets the form class name |
|---|
| 733 |
* |
|---|
| 734 |
* @return string The form class name associated with this generator |
|---|
| 735 |
*/ |
|---|
| 736 |
public function getFormClassName() |
|---|
| 737 |
{ |
|---|
| 738 |
return isset($this->params['form_class']) ? $this->params['form_class'] : $this->getClassName().'Form'; |
|---|
| 739 |
} |
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
* Retrieves all hidden fields in the widget schema |
|---|
| 743 |
* |
|---|
| 744 |
* @return array |
|---|
| 745 |
*/ |
|---|
| 746 |
public function getHiddenFields() |
|---|
| 747 |
{ |
|---|
| 748 |
$form = $this->getFormObject(); |
|---|
| 749 |
$hiddenFields = array(); |
|---|
| 750 |
foreach ($form->getWidgetSchema()->getPositions() as $name) |
|---|
| 751 |
{ |
|---|
| 752 |
if ($form[$name]->isHidden()) |
|---|
| 753 |
{ |
|---|
| 754 |
$hiddenFields[] = $name; |
|---|
| 755 |
} |
|---|
| 756 |
} |
|---|
| 757 |
|
|---|
| 758 |
return $hiddenFields; |
|---|
| 759 |
} |
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
* Gets the hidden fields as a string |
|---|
| 763 |
* |
|---|
| 764 |
* @return array |
|---|
| 765 |
*/ |
|---|
| 766 |
public function getHiddenFieldsAsString() |
|---|
| 767 |
{ |
|---|
| 768 |
$hiddenFields = ''; |
|---|
| 769 |
foreach ($this->getHiddenFields() as $name) |
|---|
| 770 |
{ |
|---|
| 771 |
$hiddenFields .= ' [?php echo $form[\''.$name.'\'] ?]'."\n"; |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
return "\n".$hiddenFields; |
|---|
| 775 |
} |
|---|
| 776 |
|
|---|
| 777 |
public function getLastNonHiddenField() |
|---|
| 778 |
{ |
|---|
| 779 |
$form = $this->getFormObject(); |
|---|
| 780 |
$positions = $form->getWidgetSchema()->getPositions(); |
|---|
| 781 |
$last = count($positions) - 1; |
|---|
| 782 |
for ($i = count($positions) - 1; $i >= 0; $i--) |
|---|
| 783 |
{ |
|---|
| 784 |
if ($form[$positions[$i]]->isHidden()) |
|---|
| 785 |
{ |
|---|
| 786 |
$last = $i - 1; |
|---|
| 787 |
} |
|---|
| 788 |
else |
|---|
| 789 |
{ |
|---|
| 790 |
break; |
|---|
| 791 |
} |
|---|
| 792 |
} |
|---|
| 793 |
|
|---|
| 794 |
return $last; |
|---|
| 795 |
} |
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
* Gets the HTML to add to the form tag if the form is multipart. |
|---|
| 799 |
* |
|---|
| 800 |
* @return string |
|---|
| 801 |
*/ |
|---|
| 802 |
public function getFormMultipartHtml() |
|---|
| 803 |
{ |
|---|
| 804 |
if (isset($this->params['non_verbose_templates']) && $this->params['non_verbose_templates']) |
|---|
| 805 |
{ |
|---|
| 806 |
return '[?php $form->isMultipart() and print \' enctype="multipart/form-data"\' ?]'; |
|---|
| 807 |
} |
|---|
| 808 |
else |
|---|
| 809 |
{ |
|---|
| 810 |
return $this->getFormObject()->isMultipart() ? ' enctype="multipart/form-data"' : ''; |
|---|
| 811 |
} |
|---|
| 812 |
} |
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
* Escapes a string. |
|---|
| 816 |
* |
|---|
| 817 |
* @param string $string |
|---|
| 818 |
*/ |
|---|
| 819 |
protected function escapeString($string) |
|---|
| 820 |
{ |
|---|
| 821 |
return preg_replace('/\'/', '\\\'', $string); |
|---|
| 822 |
} |
|---|
| 823 |
} |
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
class sfAdminColumn |
|---|
| 834 |
{ |
|---|
| 835 |
protected |
|---|
| 836 |
$phpName = '', |
|---|
| 837 |
$column = null, |
|---|
| 838 |
$flags = array(); |
|---|
| 839 |
|
|---|
| 840 |
|
|---|
| 841 |
* Constructor. |
|---|
| 842 |
* |
|---|
| 843 |
* @param string $phpName The column php name |
|---|
| 844 |
* @param string $column The column name |
|---|
| 845 |
* @param array $flags The column flags |
|---|
| 846 |
*/ |
|---|
| 847 |
public function __construct($phpName, $column = null, $flags = array()) |
|---|
| 848 |
{ |
|---|
| 849 |
$this->phpName = $phpName; |
|---|
| 850 |
$this->column = $column; |
|---|
| 851 |
$this->flags = (array) $flags; |
|---|
| 852 |
} |
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
* Returns true if the column maps a database column. |
|---|
| 856 |
* |
|---|
| 857 |
* @return boolean true if the column maps a database column, false otherwise |
|---|
| 858 |
*/ |
|---|
| 859 |
public function isReal() |
|---|
| 860 |
{ |
|---|
| 861 |
return $this->column ? true : false; |
|---|
| 862 |
} |
|---|
| 863 |
|
|---|
| 864 |
|
|---|
| 865 |
* Gets the name of the column. |
|---|
| 866 |
* |
|---|
| 867 |
* @return string The column name |
|---|
| 868 |
*/ |
|---|
| 869 |
public function getName() |
|---|
| 870 |
{ |
|---|
| 871 |
return sfInflector::underscore($this->phpName); |
|---|
| 872 |
} |
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
* Returns true if the column is a partial. |
|---|
| 876 |
* |
|---|
| 877 |
* @return boolean true if the column is a partial, false otherwise |
|---|
| 878 |
*/ |
|---|
| 879 |
public function isPartial() |
|---|
| 880 |
{ |
|---|
| 881 |
return in_array('_', $this->flags) ? true : false; |
|---|
| 882 |
} |
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
* Returns true if the column is a component. |
|---|
| 886 |
* |
|---|
| 887 |
* @return boolean true if the column is a component, false otherwise |
|---|
| 888 |
*/ |
|---|
| 889 |
public function isComponent() |
|---|
| 890 |
{ |
|---|
| 891 |
return in_array('~', $this->flags) ? true : false; |
|---|
| 892 |
} |
|---|
| 893 |
|
|---|
| 894 |
|
|---|
| 895 |
* Returns true if the column has a link. |
|---|
| 896 |
* |
|---|
| 897 |
* @return boolean true if the column has a link, false otherwise |
|---|
| 898 |
*/ |
|---|
| 899 |
public function isLink() |
|---|
| 900 |
{ |
|---|
| 901 |
return (in_array('=', $this->flags) || $this->isPrimaryKey()) ? true : false; |
|---|
| 902 |
} |
|---|
| 903 |
|
|---|
| 904 |
|
|---|
| 905 |
* Gets the php name of the column. |
|---|
| 906 |
* |
|---|
| 907 |
* @return string The php name |
|---|
| 908 |
*/ |
|---|
| 909 |
public function getPhpName() |
|---|
| 910 |
{ |
|---|
| 911 |
return $this->phpName; |
|---|
| 912 |
} |
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
public function __call($name, $arguments) |
|---|
| 916 |
{ |
|---|
| 917 |
return $this->column ? $this->column->$name() : null; |
|---|
| 918 |
} |
|---|
| 919 |
} |
|---|
| 920 |
|
|---|