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

Revision 2593, 1.8 kB (checked in by Geert, 2 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 checklist 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_Checklist_Core extends Form_Input {
13
14     protected $data = array
15     (
16         'name'    => '',
17         'type'    => 'checkbox',
18         'class'   => 'checklist',
19         'options' => array(),
20     );
21
22     protected $protect = array('name', 'type');
23
24     public function __construct($name)
25     {
26         $this->data['name'] = $name;
27     }
28
29     public function __get($key)
30     {
31         if ($key == 'value')
32         {
33             // Return the currently checked values
34             $array = array();
35             foreach ($this->data['options'] as $id => $opt)
36             {
37                 // Return the options that are checked
38                 ($opt[1] === TRUE) and $array[] = $id;
39             }
40             return $array;
41         }
42
43         return parent::__get($key);
44     }
45
46     public function render()
47     {
48         // Import base data
49         $base_data = $this->data;
50
51         // Make it an array
52         $base_data['name'] .= '[]';
53
54         // Newline
55         $nl = "\n";
56
57         $checklist = '<ul class="'.arr::remove('class', $base_data).'">'.$nl;
58         foreach (arr::remove('options', $base_data) as $val => $opt)
59         {
60             // New set of input data
61             $data = $base_data;
62
63             // Get the title and checked status
64             list ($title, $checked) = $opt;
65
66             // Set the name, value, and checked status
67             $data['value']   = $val;
68             $data['checked'] = $checked;
69
70             $checklist .= '<li><label>'.form::checkbox($data).' '.$title.'</label></li>'.$nl;
71         }
72         $checklist .= '</ul>';
73
74         return $checklist;
75     }
76
77     protected function load_value()
78     {
79         foreach ($this->data['options'] as $val => $checked)
80         {
81             if ($input = $this->input_value($this->data['name']))
82             {
83                 $this->data['options'][$val][1] = in_array($val, $input);
84             }
85             else
86             {
87                 $this->data['options'][$val][1] = FALSE;
88             }
89         }
90     }
91
92 } // End Form Checklist
Note: See TracBrowser for help on using the browser.