Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CNDeviceTarget | |
0.00% |
0 / 32 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| getAvailableDevices | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| getDevicesAssociatedWithBanner | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| 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::getReplicaDb(); |
| 18 | |
| 19 | $devices = []; |
| 20 | |
| 21 | $res = $dbr->newSelectQueryBuilder() |
| 22 | ->select( [ 'dev_id', 'dev_name', 'dev_display_label' ] ) |
| 23 | ->from( 'cn_known_devices' ) |
| 24 | ->caller( __METHOD__ ) |
| 25 | ->fetchResultSet(); |
| 26 | |
| 27 | foreach ( $res as $row ) { |
| 28 | if ( $flip ) { |
| 29 | $devices[ $row->dev_name ] = [ |
| 30 | 'label' => $row->dev_display_label, |
| 31 | 'id' => intval( $row->dev_id ), |
| 32 | ]; |
| 33 | } else { |
| 34 | $devices[ intval( $row->dev_id ) ] = [ |
| 35 | 'header' => $row->dev_name, |
| 36 | 'label' => $row->dev_display_label, |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | |
| 42 | return $devices; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Obtain all device IDs associated with a given banner ID |
| 47 | * |
| 48 | * @param int $bannerId |
| 49 | * |
| 50 | * @return array Device names that are associated with the banner |
| 51 | */ |
| 52 | public static function getDevicesAssociatedWithBanner( $bannerId ) { |
| 53 | $dbr = CNDatabase::getReplicaDb(); |
| 54 | |
| 55 | $devices = []; |
| 56 | |
| 57 | $res = $dbr->newSelectQueryBuilder() |
| 58 | ->select( [ 'devices.dev_id', 'dev_name' ] ) |
| 59 | ->from( 'cn_template_devices', 'tdev' ) |
| 60 | ->join( 'cn_known_devices', 'devices', 'tdev.dev_id = devices.dev_id' ) |
| 61 | ->where( [ |
| 62 | 'tdev.tmp_id' => $bannerId, |
| 63 | ] ) |
| 64 | ->caller( __METHOD__ ) |
| 65 | ->fetchResultSet(); |
| 66 | |
| 67 | foreach ( $res as $row ) { |
| 68 | $devices[ intval( $row->dev_id ) ] = $row->dev_name; |
| 69 | } |
| 70 | |
| 71 | return $devices; |
| 72 | } |
| 73 | } |