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