1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cApiActionCollection extends ItemCollection {
24:
25: 26: 27:
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['actions'], 'idaction');
31: $this->_setItemClass('cApiAction');
32:
33:
34: $this->_setJoinPartner('cApiAreaCollection');
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: public function create($area, $name, $alt_name = '', $code = '', $location = '', $relevant = 1) {
50: $item = $this->createNewItem();
51:
52: if (is_string($area)) {
53: $c = new cApiArea();
54: $c->loadBy('name', $area);
55: if ($c->isLoaded()) {
56: $area = $c->get('idarea');
57: } else {
58: $area = 0;
59: cWarning(__FILE__, __LINE__, "Could not resolve area [$area] passed to method [create], assuming 0");
60: }
61: }
62:
63: if (is_string($area)) {
64: $area = $this->escape($area);
65: }
66: if (is_string($name)) {
67: $name = $this->escape($name);
68: }
69: if (is_string($alt_name)) {
70: $alt_name = $this->escape($alt_name);
71: }
72:
73: $item->set('idarea', $area);
74: $item->set('name', $name);
75: $item->set('alt_name', $alt_name);
76: $item->set('code', $code);
77: $item->set('location', $location);
78: $item->set('relevant', $relevant);
79:
80: $item->store();
81:
82: return $item;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function getAvailableActions() {
92: global $cfg;
93:
94: $sql = "SELECT action.idaction, action.name, area.name AS areaname
95: FROM `%s` AS action LEFT JOIN `%s` AS area
96: ON area.idarea = action.idarea
97: WHERE action.relevant = 1 ORDER BY action.name;";
98:
99: $this->db->query($sql, $this->table, $cfg['tab']['area']);
100:
101: $actions = array();
102:
103: while ($this->db->nextRecord()) {
104: $newentry['name'] = $this->db->f('name');
105: $newentry['areaname'] = $this->db->f('areaname');
106: $actions[$this->db->f('idaction')] = $newentry;
107: }
108:
109: return $actions;
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function getActionName($action) {
120: $this->db->query("SELECT name FROM `%s` WHERE idaction = %d", $this->table, $action);
121:
122: return ($this->db->nextRecord()) ? $this->db->f('name') : NULL;
123: }
124:
125: 126: 127: 128: 129: 130: 131: 132:
133: function getAreaForAction($action) {
134: if (!is_numeric($action)) {
135: $this->db->query("SELECT idarea FROM `%s` WHERE name = '%s'", $this->table, $action);
136: } else {
137: $this->db->query("SELECT idarea FROM `%s` WHERE idaction = %d", $this->table, $action);
138: }
139:
140: return ($this->db->nextRecord()) ? $this->db->f('idarea') : NULL;
141: }
142: }
143:
144: 145: 146: 147: 148: 149:
150: class cApiAction extends Item {
151:
152: 153: 154: 155: 156: 157:
158: public function __construct($mId = false) {
159: global $cfg;
160:
161: parent::__construct($cfg['tab']['actions'], 'idaction');
162: $this->setFilters(array(
163: 'addslashes'
164: ), array(
165: 'stripslashes'
166: ));
167:
168: if ($mId !== false) {
169: $this->loadByPrimaryKey($mId);
170: }
171:
172:
173: $this->_wantParameters = array();
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public function setField($name, $value, $bSafe = true) {
186: switch ($name) {
187: case 'relevant':
188: $value = (int) $value;
189: break;
190: }
191:
192: return parent::setField($name, $value, $bSafe);
193: }
194:
195: }
196: