1 <?php
2 /**
3 * Authorization item data provider class file.
4 *
5 * @author Christoffer Niska <cniska@live.com>
6 * @copyright Copyright © 2010 Christoffer Niska
7 * @since 0.9.10
8 */
9 class RAuthItemDataProvider extends CDataProvider
10 {
11 public $type;
12 public $userId;
13 public $parent;
14 public $exclude = array();
15 public $items;
16 public $sortable;
17
18 /**
19 * Constructs the data provider.
20 * @param string $id the data provider identifier.
21 * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
22 * @return RightsAuthItemDataProvider
23 */
24 public function __construct($id, $config=array())
25 {
26 $this->setId($id);
27
28 foreach($config as $key=>$value)
29 $this->$key=$value;
30 }
31
32 /**
33 * Fetches the data from the persistent data storage.
34 * @return array list of data items
35 */
36 public function fetchData()
37 {
38 if( $this->sortable!==null )
39 $this->processSortable();
40
41 if( $this->items===null )
42 $this->items = Rights::getAuthorizer()->getAuthItems($this->type, $this->userId, $this->parent, true, $this->exclude);
43
44 $data = array();
45 foreach( $this->items as $name=>$item )
46 $data[] = $item;
47
48 return $data;
49 }
50
51 /**
52 * Fetches the data item keys from the persistent data storage.
53 * @return array list of data item keys.
54 */
55 public function fetchKeys()
56 {
57 $keys = array();
58 foreach( $this->getData() as $name=>$item )
59 $keys[] = $name;
60
61 return $keys;
62 }
63
64 /**
65 * Applies jQuery UI sortable on the target element.
66 */
67 protected function processSortable()
68 {
69 if( $this->sortable!==null )
70 {
71 if( isset($this->sortable['id'])===true && isset($this->sortable['element'])===true && isset($this->sortable['url'])===true )
72 {
73 // Register the script to bind the sortable plugin to the role table
74 Yii::app()->getClientScript()->registerScript($this->sortable['id'],
75 "jQuery('".$this->sortable['element']."').rightsSortableTable({
76 url:'".$this->sortable['url']."',
77 csrfToken:'".Yii::app()->request->csrfToken."'
78 });"
79 );
80 }
81 }
82 }
83
84 /**
85 * Calculates the total number of data items.
86 * @return integer the total number of data items.
87 */
88 protected function calculateTotalItemCount()
89 {
90 return count($this->getData());
91 }
92 }
93