1: <?php
2: /**
3: * Contains the DataBinder trait
4: */
5:
6: /**
7: * Trait QDataBinder
8: * The QDataBinder trait encapsulates the functionality of data binding for multiple controls that would like to make
9: * use of it. Data binding lets you only recall data during draw time, thus saving space, because the data is not
10: * saved with the formstate. Controls that use this will have to be sure to unload the data after drawing as needed.
11: *
12: * There are a couple of modes of this. A legacy mode use strings to record the function names. The more modern way uses
13: * an array as a callable.
14: */
15: trait QDataBinder {
16: protected $objDataBinder;
17:
18: /**
19: * Sets the data binder. Allows it to be sent in a couple of different ways:
20: * - legacy mode: method name followed by optional control to call the method on. If not specified, will call on the form.
21: * - modern mode: a php callable.
22: * @param callable|string $mixMethodName
23: * @param null|QForm|QControl $objParentControl
24: */
25: public function SetDataBinder($mixMethodName, $objParentControl = null) {
26: if (is_callable($mixMethodName)) {
27: $this->objDataBinder = $mixMethodName;
28: } elseif (is_string ($mixMethodName)) {
29: if (!$objParentControl) {
30: $objParentControl = $this->GetForm();
31: }
32: $this->objDataBinder = array($objParentControl, $mixMethodName);
33: }
34: else {
35: assert('false'); // Improperly specified data binder
36: }
37: }
38:
39: /**
40: * Bind the data by calling the data binder. Will pass the current control as the parameter to the data binder.
41: * @throws Exception
42: * @throws QCallerException
43: */
44: public function CallDataBinder() {
45: if ($this->objDataBinder) {
46: if (is_array($this->objDataBinder)) {
47: if ($this->objDataBinder[0] === $this) {
48: call_user_func($this->objDataBinder); // assume data binder is in a qcontrol, or is public
49: }
50: elseif ($this->objDataBinder[0] instanceof QForm) {
51: $this->objDataBinder[0]->CallDataBinder($this->objDataBinder, $this); // Let form call the data binder, so that binder can be private to form
52: } else {
53: call_user_func($this->objDataBinder, $this); // assume data binder is in a qcontrol, or is public
54: }
55: }
56: else {
57: try {
58: call_user_func($this->objDataBinder); // calling databinder on self, so do not pass the control as param
59: } catch (QCallerException $objExc) {
60: $objExc->IncrementOffset();
61: throw $objExc;
62: }
63: }
64: }
65: }
66:
67: /**
68: * Check the binder for a reference to the form.
69: */
70: public function Sleep() {
71: $this->objDataBinder = QControl::SleepHelper ($this->objDataBinder);
72: parent::Sleep();
73: }
74:
75: /**
76: * @param QForm $objForm
77: */
78: public function Wakeup(QForm $objForm) {
79: parent::Wakeup($objForm);
80: $this->objDataBinder = QControl::WakeupHelper ($objForm, $this->objDataBinder);
81: }
82:
83: /**
84: * @return bool
85: */
86: public function HasDataBinder() {
87: return $this->objDataBinder ? true : false;
88: }
89: /**
90: * Returns the QForm. All QControls implement this.
91: * @return QForm
92: */
93: abstract function GetForm();
94: }