aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/DataFieldPhoneEntry.class.php
blob: d3a0e705e806f2852bccf579f93228bba0b9723e (plain)
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
# Lifter002: DONE
# Lifter007: TEST

/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author  Marcus Lunzenauer <mlunzena@uos.de>
 * @author  Martin Gieseking  <mgieseki@uos.de>
 * @license GPL2 or any later version
 */
class DataFieldPhoneEntry extends DataFieldEntry
{
    protected $template = 'phone.php';

    /**
     * Returns the number of html fields this datafield uses for input.
     *
     * @return int representing the number of html fields
     */
    public function numberOfHTMLFields()
    {
        return 3;
    }

    /**
     * Sets the value from a post request
     *
     * @param mixed $submitted_value The value from request
     */
    public function setValueFromSubmit($value)
    {
        if (is_array($value)) {
            $value = array_slice($value, 0, 3);
            $value = implode("\n", $value);
            $value = str_replace(' ', '', $value);

            parent::setValueFromSubmit($value);
        }
    }

    /**
     * Returns the display/rendered value of this datafield
     *
     * @param bool $entities Should html entities be encoded (defaults to true)
     * @return String containg the rendered value
     */
    public function getDisplayValue($entities = true)
    {
        list($country, $area, $phone) = $this->getNumberParts();

        if ($country || $area || $phone) {
            if ($country) {
                $country = "+$country";
            }

            return "$country $area $phone";
        }

        return '';
    }

    /**
     * Returns the according input elements as html for this datafield
     *
     * @param String $name      Name prefix of the associated input
     * @param Array  $variables Additional variables
     * @return String containing the required html
     */
    public function getHTML($name = '', $variables = [])
    {
        return parent::getHTML($name, $variables + [
            'values' => $this->getNumberParts()
        ]);
    }

    /**
     * Checks if the datafield is empty (was not set)
     *
     * @return bool true if empty, else false
     */
    public function isEmpty()
    {
        return $this->getValue() == "\n\n";
    }

    /**
     * Returns whether the datafield contents are valid
     *
     * @return boolean indicating whether the datafield contents are valid
     */
    public function isValid()
    {
        $value = trim($this->value);

        if (!$value) {
            return parent::isValid();
        }

        return parent::isValid()
            && preg_match('/^([1-9]\d*)?\n[1-9]\d+\n[1-9]\d+(-\d+)?$/', $value);
    }

    /**
     * Retturns the individual parts of the telephone number.
     * The resulting array is always padded to contain at least
     * three items.
     *
     * @return array containing the individual parts.
     */
    protected function getNumberParts()
    {
        $values = explode("\n", $this->value);

        // pad values array to a size of 3 by inserting empty values from left
        while (count($values) < 3) {
            array_unshift($values, '');
        }

        return $values;
    }
}