1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29:
30: class AjaxFormComponentUpdateBehavior extends AjaxEventBehaviour
31: {
32:
33: public function __construct($event, $onEvent, $onSubmit, $onError)
34: {
35: Args::callBackArgs($onEvent, 1, 'onEvent');
36:
37: if($onSubmit!=null)
38: {
39: Args::callBackArgs($onSubmit, 1, 'onSubmit');
40: }
41: if($onError!=null)
42: {
43: Args::callBackArgs($onError, 1, 'onError');
44: }
45:
46: $self = $this;
47: parent::__construct($event, function($target) use ($self, $onEvent, $onSubmit, $onError)
48: {
49: $formComponent = $self->getComponent();
50: $formComponent->processInput();
51: $onEvent($target);
52:
53: if($formComponent->isValid() && $onSubmit!=null)
54: {
55: $onSubmit($target);
56: }
57: if(!$formComponent->isValid() && $onError!=null)
58: {
59: $onError($target);
60: }
61: });
62: }
63:
64: public function bind(Component &$component)
65: {
66: if(!($component instanceof FormComponent))
67: {
68: throw new \InvalidArgumentException('AjaxFormComponentUpdateBehavior can only be added to a form component');
69: }
70: parent::bind($component);
71: }
72:
73: protected function generateCallScript($url)
74: {
75: return sprintf("piconAjaxSubmit('%s', '%s'", $this->getSubmitForm()->getMarkupId(), $url);
76: }
77:
78: private function getSubmitForm()
79: {
80: $usingComponent = $this->form;
81: $form = $this->form;
82: if($usingComponent==null)
83: {
84: $usingComponent = $this->getComponent();
85: }
86:
87: $callback = function(&$component) use (&$form)
88: {
89: if($component instanceof Form)
90: {
91: $form = $component;
92: }
93: if($component instanceof ModalWindow)
94: {
95: return Component::VISITOR_STOP_TRAVERSAL;
96: }
97:
98: return Component::VISITOR_CONTINUE_TRAVERSAL;
99: };
100: $usingComponent->visitParents(Component::getIdentifier(), $callback);
101: return $form;
102: }
103: }
104:
105: ?>
106: