|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Render a button that will submit the form its contained in through ajax. |
|
5
|
|
|
* |
|
6
|
|
|
* @see framework/client/dist/js/InlineFormAction.js |
|
7
|
|
|
* |
|
8
|
|
|
* @package forms |
|
9
|
|
|
* @subpackage actions |
|
10
|
|
|
*/ |
|
11
|
|
|
class InlineFormAction extends FormField { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create a new action button. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $action The method to call when the button is clicked |
|
17
|
|
|
* @param string $title The label on the button |
|
18
|
|
|
* @param string $extraClass A CSS class to apply to the button in addition to 'action' |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($action, $title = "", $extraClass = '') { |
|
21
|
|
|
$this->extraClass = ' '.$extraClass; |
|
22
|
|
|
parent::__construct($action, $title); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function performReadonlyTransformation() { |
|
26
|
|
|
return $this->castedCopy('InlineFormAction_ReadOnly'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $properties |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function Field($properties = array()) { |
|
34
|
|
|
return FormField::create_tag('input', array( |
|
|
|
|
|
|
35
|
|
|
'type' => 'submit', |
|
36
|
|
|
'name' => sprintf('action_%s', $this->getName()), |
|
37
|
|
|
'value' => $this->title, |
|
38
|
|
|
'id' => $this->ID(), |
|
39
|
|
|
'class' => sprintf('action%s', $this->extraClass), |
|
40
|
|
|
)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function Title() { |
|
44
|
|
|
return false; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Readonly version of {@link InlineFormAction}. |
|
50
|
|
|
* @package forms |
|
51
|
|
|
* @subpackage actions |
|
52
|
|
|
*/ |
|
53
|
|
|
class InlineFormAction_ReadOnly extends FormField { |
|
54
|
|
|
|
|
55
|
|
|
protected $readonly = true; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param array $properties |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function Field($properties = array()) { |
|
62
|
|
|
return FormField::create_tag('input', array( |
|
|
|
|
|
|
63
|
|
|
'type' => 'submit', |
|
64
|
|
|
'name' => sprintf('action_%s', $this->name), |
|
65
|
|
|
'value' => $this->title, |
|
66
|
|
|
'id' => $this->ID(), |
|
67
|
|
|
'disabled' => 'disabled', |
|
68
|
|
|
'class' => 'action disabled ' . $this->extraClass, |
|
69
|
|
|
)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function Title() { |
|
73
|
|
|
return false; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.