Completed
Push — master ( 189df8...8ba53b )
by Tolan
07:59
created

ExecHelper::replaceElementUsingLink()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 2
eloc 10
nc 2
nop 5
1
<?php
2
/*
3
 * This file is part of the Patternseek ComponentView library.
4
 *
5
 * (c)2014 Tolan Blundell <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PatternSeek\ComponentView;
12
13
class ExecHelper
14
{
15
16
    /**
17
     * @var AbstractViewComponent
18
     */
19
    protected $component;
20
21
    public function setComponent( AbstractViewComponent $component )
22
    {
23
        $this->component = $component;
24
    }
25
26
    /**
27
     * Generate and return an appropriate URL or URI to call the exec handler identified by $execPath with $args
28
     * @param string $execMethod Component hierarchy path to an exec handler function on a component
29
     * @param array $args Arguments to be passed to the called handler
30
     * @param bool $onlyComponentOutput
31
     * @return string A URL or URI
32
     */
33
    public function url( $execMethod, $args = [ ], $onlyComponentOutput = false ) // $onlyComponentOutput Not used in this implementation but necessary for subclasses
0 ignored issues
show
Unused Code introduced by
The parameter $onlyComponentOutput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        // $onlyComponentOutput is not used in this implementation but may be by sub-classes
36
        $args[ 'exec' ] = $this->component->getExecPath( $execMethod );
37
        $qs = http_build_query( $args );
38
        return "?{$qs}";
39
    }
40
41
    /**
42
     * Generate and return a form, wrapping $formBody to call the exec handler identified by $execPath using the $method HTTP method
43
     * @param string $execMethod Component hierarchy path to an exec handler function on a component
44
     * @param string $method
45
     * @param string $formBody The body of an HTML form to be wrapped
46
     * @param bool $onlyComponentOutput
47
     * @param null $formID
48
     * @param null $onSubmit
49
     * @param string $encType
50
     * @return string HTML form
51
     */
52
    public function wrapForm(
53
        $execMethod,
54
        $method,
55
        $formBody,
56
        $onlyComponentOutput = false, // Not used in this implementation but necessary for subclasses
0 ignored issues
show
Unused Code introduced by
The parameter $onlyComponentOutput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
        $formID = null,
58
        $onSubmit = null,
59
        $encType = 'application/x-www-form-urlencoded'
60
    )
61
    {
62
        // $onlyComponentOutput is not used in this implementation but may be by sub-classes
63
        if (null !== $formID) {
64
            $formID = " id='{$formID}'";
65
        }
66
        if (null !== $onSubmit) {
67
            $onSubmit = " onsubmit='{$onSubmit}'";
68
        }
69
        return <<<EOS
70
<form method="{$method}" action=""{$formID}{$onSubmit} enctype="{$encType}">
71
    <input type="hidden" name="exec" value="{$this->component->getExecPath( $execMethod )}">
72
    {$formBody}
73
</form>
74
EOS;
75
    }
76
77
    /**
78
     * Helper for calling static methods
79
     * @param $class
80
     * @param $function
81
     * @param array $args
82
     * @return mixed|null
83
     */
84
    function callStatic($class, $function, $args = array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
    {
86
        if (class_exists($class) && method_exists($class, $function)) {
87
            return call_user_func_array( array( $class, $function ), $args );
88
        }
89
        return null;
90
    }
91
92
    /**
93
     * Generate a link which replaces the content of a DOM element with the output of an exec method
94
     * @param $execMethod
95
     * @param array $args
96
     * @param $targetDiv
97
     * @param $linkText
98
     * @param array $anchorAttrs
99
     * @return string
100
     */
101
    public function replaceElementUsingLink( $execMethod, $args = [ ], $targetDiv, $linkText, $anchorAttrs = [ ] )
102
    {
103
        $url = $this->url( $execMethod, $args, true );
104
        $attrs = [ ];
105
        foreach ($anchorAttrs as $k => $v) {
106
            $attrs[ ] = "{$k}='{$v}'";
107
        }
108
        $attrsStr = implode( ' ', $attrs );
109
        return <<<EOS
110
        <script type="application/javascript">
111
            if( typeof(execLink) != "function" ){
112
                var execLink = function( url, targetDiv ){
113
                    // Send the data using post
114
                    var posting = $.get( url, $( form ).serialize() );
115
                 
116
                    // Put the results in a div
117
                    posting.done(function( data ) {
118
                        $( "#"+targetDiv ).replaceWith( data );
119
                        $("body").css("cursor", "default");
120
                    });
121
    
122
                    // Show optional progress
123
                    $("body").css("cursor", "progress");
124
                }
125
            }
126
        </script>
127
        <a href="#" onclick="execLink( '{$url}', '{$targetDiv}' ); return false;" {$attrsStr}>{$linkText}</a>
128
EOS;
129
    }
130
131
    /**
132
     * Generate a form which replaces the content of a DOM element with the output of an exec method
133
     * @param $execMethod
134
     * @param $method
135
     * @param string $formBody The body of an HTML form to be wrapped
136
     * @param $targetDiv
137
     * @param $formID
138
     * @param string $encType
139
     * @return string
140
     */
141
    public function replaceElementUsingForm( $execMethod, $method, $formBody, $targetDiv, $formID, $encType = 'application/x-www-form-urlencoded' )
142
    {
143
        return <<<EOS
144
        {$this->wrapForm( $execMethod, $method, $formBody, true, $formID,
145
            "execForm( this, \"{$targetDiv}\" ); return false;", $encType )}
146
        <script type="application/javascript">
147
        if( typeof(execForm) != "function" ){
148
            var execForm = function( form, targetDiv ){
149
            
150
                // Send the data using post
151
                var posting = $.post( [location.protocol, '//', location.host, location.pathname].join(''), $( form ).serialize() );
152
             
153
                // Put the results in a div
154
                posting.done(function( data ) {
155
                    $( "#"+targetDiv ).replaceWith( data );
156
                    $("body").css("cursor", "default");
157
                });
158
159
                // Show optional progress
160
                $("body").css("cursor", "progress");
161
            }
162
        }
163
        </script>
164
EOS;
165
    }
166
}
167