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