1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cApiActionCollection extends ItemCollection {
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: public function create($area, $name, $alt_name = '', $code = '', $location = '', $relevant = 1) {
51: $item = $this->createNewItem();
52:
53: if (is_string($area)) {
54: $c = new cApiArea();
55: $c->loadBy('name', $area);
56: if ($c->isLoaded()) {
57: $area = $c->get('idarea');
58: } else {
59: $area = 0;
60: cWarning(__FILE__, __LINE__, "Could not resolve area [$area] passed to method [create], assuming 0");
61: }
62: }
63:
64: if (is_string($area)) {
65: $area = $this->escape($area);
66: }
67: if (is_string($name)) {
68: $name = $this->escape($name);
69: }
70: if (is_string($alt_name)) {
71: $alt_name = $this->escape($alt_name);
72: }
73:
74: $item->set('idarea', $area);
75: $item->set('name', $name);
76: $item->set('alt_name', $alt_name);
77: $item->set('code', $code);
78: $item->set('location', $location);
79: $item->set('relevant', $relevant);
80:
81: $item->store();
82:
83: return $item;
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: 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: protected $_objectInvalid = false;
157:
158: 159: 160: 161: 162:
163: public function __construct($mId = false) {
164: global $cfg;
165:
166: parent::__construct($cfg['tab']['actions'], 'idaction');
167: $this->setFilters(array(
168: 'addslashes'
169: ), array(
170: 'stripslashes'
171: ));
172:
173: if ($mId !== false) {
174: $this->loadByPrimaryKey($mId);
175: }
176:
177:
178: $this->_wantParameters = array();
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public function setField($name, $value, $bSafe = true) {
189: switch ($name) {
190: case 'relevant':
191: $value = (int) $value;
192: break;
193: }
194:
195: return parent::setField($name, $value, $bSafe);
196: }
197:
198: }
199: