root/trunk/modules/forge/libraries/Form_Phonenumber.php

Revision 2593, 2.2 kB (checked in by Geert, 3 months ago)

CodingStyle maintenance!

  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007-2008 Kohana Team
  • Property svn:keywords set to Id
Line 
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3  * FORGE phone number input library.
4  *
5  * $Id$
6  *
7  * @package    Forge
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class Form_Phonenumber_Core extends Form_Input {
13
14     protected $data = array
15     (
16         'name'  => '',
17         'class' => 'phone_number',
18     );
19
20     protected $protect = array('type');
21
22     // Precision for the parts, you can use @ to insert a literal @ symbol
23     protected $parts = array
24     (
25         'area_code'   => '',
26         'exchange'     => '',
27         'last_four'    => '',
28     );
29
30     public function __construct($name)
31     {
32         // Set name
33         $this->data['name'] = $name;
34     }
35
36     public function __call($method, $args)
37     {
38         if (isset($this->parts[substr($method, 0, -1)]))
39         {
40             // Set options for date generation
41             $this->parts[substr($method, 0, -1)] = $args;
42             return $this;
43         }
44
45         return parent::__call($method, $args);
46     }
47
48     public function html_element()
49     {
50         // Import base data
51         $data = $this->data;
52
53         $input = '';
54         foreach ($this->parts as $type => $val)
55         {
56             isset($data['value']) OR $data['value'] = '';
57             $temp = $data;
58             $temp['name'] = $this->data['name'].'['.$type.']';
59             $offset = (strlen($data['value']) == 10) ? 0 : 3;
60             switch ($type)
61             {
62                 case 'area_code':
63                     if (strlen($data['value']) == 10)
64                     {
65                         $temp['value'] = substr($data['value'], 0, 3);
66                     }
67                     else
68                         $temp['value'] = '';
69                     $temp['class'] = 'area_code';
70                     $input .= form::input(array_merge(array('value' => $val), $temp)).'-';
71                     break;
72                 case 'exchange':
73                     $temp['value'] = substr($data['value'], (3-$offset), 3);
74                     $temp['class'] = 'exchange';
75                     $input .= form::input(array_merge(array('value' => $val), $temp)).'-';
76                     break;
77                 case 'last_four':
78                     $temp['value'] = substr($data['value'], (6-$offset), 4);
79                     $temp['class'] = 'last_four';
80                     $input .= form::input(array_merge(array('value' => $val), $temp));
81                     break;
82             }
83             
84         }
85
86         return $input;
87     }
88
89     protected function load_value()
90     {
91         if (is_bool($this->valid))
92             return;
93
94         $data = $this->input_value($this->name, $this->data['name']);
95
96         $this->data['value'] = $data['area_code'].$data['exchange'].$data['last_four'];
97     }
98 } // End Form Phonenumber
Note: See TracBrowser for help on using the browser.