Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CNDeviceTarget | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
getAvailableDevices | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
getDevicesAssociatedWithBanner | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
addDeviceTarget | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
setBannerDeviceTargets | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /** |
4 | * Manages device targeting for CentralNotice banners |
5 | */ |
6 | class CNDeviceTarget { |
7 | /** |
8 | * Get a listing of all known targetable devices. |
9 | * |
10 | * @param bool $flip If true will return |
11 | * {<header string value>: {'id': <id>, 'label': <wiki text label>}} |
12 | * |
13 | * @return array[] Array of devices in format |
14 | * {id: {'header': <internal string value>, 'label': <wiki text label>}} |
15 | */ |
16 | public static function getAvailableDevices( $flip = false ) { |
17 | $dbr = CNDatabase::getDb(); |
18 | |
19 | $devices = []; |
20 | |
21 | $res = $dbr->select( |
22 | [ 'known_devices' => 'cn_known_devices' ], |
23 | [ 'dev_id', 'dev_name', 'dev_display_label' ], |
24 | [], |
25 | __METHOD__ |
26 | ); |
27 | |
28 | foreach ( $res as $row ) { |
29 | if ( $flip ) { |
30 | $devices[ $row->dev_name ] = [ |
31 | 'label' => $row->dev_display_label, |
32 | 'id' => intval( $row->dev_id ), |
33 | ]; |
34 | } else { |
35 | $devices[ intval( $row->dev_id ) ] = [ |
36 | 'header' => $row->dev_name, |
37 | 'label' => $row->dev_display_label, |
38 | ]; |
39 | } |
40 | |
41 | } |
42 | |
43 | return $devices; |
44 | } |
45 | |
46 | /** |
47 | * Obtain all device IDs associated with a given banner ID |
48 | * |
49 | * @param int $bannerId |
50 | * |
51 | * @return array Device names that are associated with the banner |
52 | */ |
53 | public static function getDevicesAssociatedWithBanner( $bannerId ) { |
54 | $dbr = CNDatabase::getDb(); |
55 | |
56 | $devices = []; |
57 | |
58 | $res = $dbr->select( |
59 | [ |
60 | 'tdev' => 'cn_template_devices', |
61 | 'devices' => 'cn_known_devices' |
62 | ], |
63 | [ 'devices.dev_id', 'dev_name' ], |
64 | [ |
65 | 'tdev.tmp_id' => $bannerId, |
66 | 'tdev.dev_id = devices.dev_id' |
67 | ], |
68 | __METHOD__ |
69 | ); |
70 | |
71 | foreach ( $res as $row ) { |
72 | $devices[ intval( $row->dev_id ) ] = $row->dev_name; |
73 | } |
74 | |
75 | return $devices; |
76 | } |
77 | |
78 | /** |
79 | * Add a new device target to the database |
80 | * |
81 | * @param string $deviceName Name of the device as sent by the controller (read: MobileFrontEnd) |
82 | * @param string $displayLabel Friendly wikitext name of the device |
83 | * @return int The ID of the device added |
84 | */ |
85 | public static function addDeviceTarget( $deviceName, $displayLabel ) { |
86 | $db = CNDatabase::getDb( DB_PRIMARY ); |
87 | |
88 | $db->newInsertQueryBuilder() |
89 | ->insertInto( 'cn_known_devices' ) |
90 | ->row( [ |
91 | 'dev_name' => $deviceName, |
92 | 'dev_display_label' => $displayLabel |
93 | ] ) |
94 | ->caller( __METHOD__ ) |
95 | ->execute(); |
96 | |
97 | return $db->insertId(); |
98 | } |
99 | |
100 | /** |
101 | * Sets the associated devices with a banner |
102 | * |
103 | * @param int $bannerId Banner ID to modify |
104 | * @param string|array $newDevices Single name, or array of names, of devices that should be |
105 | * associated with a banner |
106 | * @throws RangeException |
107 | * |
108 | * FIXME Unused, remove |
109 | */ |
110 | public static function setBannerDeviceTargets( $bannerId, $newDevices ) { |
111 | $db = CNDatabase::getDb(); |
112 | |
113 | $knownDevices = self::getAvailableDevices( true ); |
114 | $newDevices = (array)$newDevices; |
115 | |
116 | // Remove all entries from the table for this banner |
117 | $db->newDeleteQueryBuilder() |
118 | ->deleteFrom( 'cn_template_devices' ) |
119 | ->where( [ 'tmp_id' => $bannerId ] ) |
120 | ->caller( __METHOD__ ) |
121 | ->execute(); |
122 | |
123 | // Add the new device mappings |
124 | if ( $newDevices ) { |
125 | $modifyArray = []; |
126 | foreach ( $newDevices as $device ) { |
127 | if ( !array_key_exists( $device, $knownDevices ) ) { |
128 | throw new RangeException( "Device name '$device' not known! Cannot add." ); |
129 | } |
130 | $modifyArray[] = [ |
131 | 'tmp_id' => $bannerId, |
132 | 'dev_id' => $knownDevices[$device]['id'] |
133 | ]; |
134 | } |
135 | $db->newInsertQueryBuilder() |
136 | ->insertInto( 'cn_template_devices' ) |
137 | ->rows( $modifyArray ) |
138 | ->caller( __METHOD__ ) |
139 | ->execute(); |
140 | } |
141 | } |
142 | } |