| 53 | rodolico | 1 | <?php
 | 
        
           |  |  | 2 |    require_once( 'camp.class.php' );
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 |    class Device extends Camp {
 | 
        
           |  |  | 5 |       protected static $dbStructure = array(
 | 
        
           |  |  | 6 |          'table' => array(
 | 
        
           |  |  | 7 |             'tableName' => 'device',
 | 
        
           |  |  | 8 |             'primaryKey' => 'device_id',
 | 
        
           |  |  | 9 |             'selectionDisplay' => 'name',
 | 
        
           | 69 | rodolico | 10 |             'inactiveField' => 'removed',
 | 
        
           | 53 | rodolico | 11 |             'fields' => array(
 | 
        
           | 56 | rodolico | 12 |                   'id' => array (
 | 
        
           |  |  | 13 |                      'fieldname' => 'device_id',
 | 
        
           | 53 | rodolico | 14 |                      'displayName' => 'ID',
 | 
        
           |  |  | 15 |                      'type' => 'int unsigned',
 | 
        
           | 63 | rodolico | 16 |                      'nullable' => false,
 | 
        
           |  |  | 17 |                      'canEdit' => false
 | 
        
           | 53 | rodolico | 18 |                   ),
 | 
        
           |  |  | 19 |                   'name' => array (
 | 
        
           | 56 | rodolico | 20 |                      'fieldname' => 'name',
 | 
        
           |  |  | 21 |                      'displayName' => 'Name',
 | 
        
           | 53 | rodolico | 22 |                      'type' => 'varchar',
 | 
        
           |  |  | 23 |                      'size' => 64,
 | 
        
           |  |  | 24 |                      'list' => true
 | 
        
           |  |  | 25 |                   ),
 | 
        
           |  |  | 26 |                   'uuid' => array (
 | 
        
           | 56 | rodolico | 27 |                      'fieldname' => 'uuid',
 | 
        
           | 53 | rodolico | 28 |                      'displayName' => 'UUID',
 | 
        
           |  |  | 29 |                      'type' => 'varchar',
 | 
        
           |  |  | 30 |                      'size' => 36,
 | 
        
           |  |  | 31 |                      'list' => false
 | 
        
           |  |  | 32 |                   ),
 | 
        
           |  |  | 33 |                   'serial' => array (
 | 
        
           | 56 | rodolico | 34 |                      'fieldname' => 'serial',
 | 
        
           |  |  | 35 |                      'displayName' => 'Serial',
 | 
        
           | 53 | rodolico | 36 |                      'type' => 'varchar',
 | 
        
           |  |  | 37 |                      'size' => 36,
 | 
        
           |  |  | 38 |                      'list' => false
 | 
        
           |  |  | 39 |                   ),
 | 
        
           |  |  | 40 |                   'created' => array (
 | 
        
           | 56 | rodolico | 41 |                      'fieldname' => 'created',
 | 
        
           | 53 | rodolico | 42 |                      'displayName' => 'Created',
 | 
        
           |  |  | 43 |                      'type' => 'date'
 | 
        
           |  |  | 44 |                   ),
 | 
        
           |  |  | 45 |                   'removed' => array(
 | 
        
           | 56 | rodolico | 46 |                      'fieldname' => 'removed',
 | 
        
           | 53 | rodolico | 47 |                      'displayName' => 'Removed',
 | 
        
           |  |  | 48 |                      'type' => 'date'
 | 
        
           | 59 | rodolico | 49 |                   ),
 | 
        
           |  |  | 50 |                   'device_types' => array(
 | 
        
           |  |  | 51 |                      'fieldname' => 'device_types',
 | 
        
           |  |  | 52 |                      'displayName' => 'Device Type',
 | 
        
           | 64 | rodolico | 53 |                      'type' => '1:M',
 | 
        
           | 65 | rodolico | 54 |                      'linkageTable' => 'device_device_type',
 | 
        
           |  |  | 55 |                      'linkageColumn' => 'device_id',
 | 
        
           |  |  | 56 |                      'foreignTable' => 'device_type',
 | 
        
           |  |  | 57 |                      'foreignColumn' => 'device_type_id',
 | 
        
           | 69 | rodolico | 58 |                      'foreignDisplayColumn' => 'name',
 | 
        
           |  |  | 59 |                      'displayView' => array (
 | 
        
           |  |  | 60 |                         'viewName' => 'device_device_types',
 | 
        
           |  |  | 61 |                         'myField' => 'device_id',
 | 
        
           |  |  | 62 |                         'displayField' => 'device_types'
 | 
        
           |  |  | 63 |                      )
 | 
        
           | 59 | rodolico | 64 |                   ),
 | 
        
           |  |  | 65 |                   'parent' => array(
 | 
        
           | 69 | rodolico | 66 |                      'displayQuery' => 'select b.device_id id, b.name display from device_device a join device b on (a.parent_id = b.device_id) where a.removed is null and a.device_id = ~~id~~',
 | 
        
           | 59 | rodolico | 67 |                      'fieldname' => 'parent_id',
 | 
        
           |  |  | 68 |                      'class' => 'Device',
 | 
        
           | 64 | rodolico | 69 |                      'displayName' => 'Parent',
 | 
        
           | 69 | rodolico | 70 |                      'displayColumn' => 'name',
 | 
        
           | 64 | rodolico | 71 |                      'type' => '1:1H',
 | 
        
           | 65 | rodolico | 72 |                      'linkageTable' => 'device_device',
 | 
        
           |  |  | 73 |                      'linkageColumn' => 'device_id',
 | 
        
           |  |  | 74 |                      'foreignColumn' => 'parent_id',
 | 
        
           | 72 | rodolico | 75 |                      'foreignTable' => 'device',
 | 
        
           |  |  | 76 |                      'inactiveField' => 'removed',
 | 
        
           |  |  | 77 |                      'addedField' => 'created'
 | 
        
           | 59 | rodolico | 78 |                   ),
 | 
        
           |  |  | 79 |                   'owner' => array(
 | 
        
           |  |  | 80 |                      'fieldname' => 'owner_id',
 | 
        
           |  |  | 81 |                      'class' => 'Owner',
 | 
        
           | 64 | rodolico | 82 |                      'displayName' => 'Owner',
 | 
        
           | 69 | rodolico | 83 |                      'displayColumn' => 'name',
 | 
        
           | 64 | rodolico | 84 |                      'type' => '1:1H',
 | 
        
           | 65 | rodolico | 85 |                      'linkageTable' => 'owner_device',
 | 
        
           |  |  | 86 |                      'linkageColumn' => 'device_id',
 | 
        
           |  |  | 87 |                      'foreignColumn' => 'owner_id',
 | 
        
           | 72 | rodolico | 88 |                      'foreignTable' => 'owner',
 | 
        
           |  |  | 89 |                      'inactiveField' => 'removed',
 | 
        
           |  |  | 90 |                      'addedField' => 'created'
 | 
        
           | 59 | rodolico | 91 |                   ),
 | 
        
           |  |  | 92 |                   'location' => array(
 | 
        
           | 64 | rodolico | 93 |                      'fieldname' => 'location_id',
 | 
        
           |  |  | 94 |                      'class' => 'Location',
 | 
        
           | 59 | rodolico | 95 |                      'displayName' => 'Location',
 | 
        
           | 69 | rodolico | 96 |                      'displayColumn' => 'name',
 | 
        
           | 64 | rodolico | 97 |                      'type' => '1:1H',
 | 
        
           | 65 | rodolico | 98 |                      'linkageTable' => 'location_device',
 | 
        
           |  |  | 99 |                      'linkageColumn' => 'device_id',
 | 
        
           |  |  | 100 |                      'foreignColumn' => 'location_id',
 | 
        
           | 72 | rodolico | 101 |                      'foreignTable' => 'location',
 | 
        
           |  |  | 102 |                      'inactiveField' => 'removed',
 | 
        
           |  |  | 103 |                      'addedField' => 'created'
 | 
        
           | 59 | rodolico | 104 |                   ),
 | 
        
           | 53 | rodolico | 105 |                ) // fields
 | 
        
           | 56 | rodolico | 106 |             ), // table
 | 
        
           | 59 | rodolico | 107 |             'children' => array(
 | 
        
           | 64 | rodolico | 108 |                'Device' => array(
 | 
        
           | 66 | rodolico | 109 |                   'name' => 'Children',
 | 
        
           | 72 | rodolico | 110 |                   'class' => 'Device',
 | 
        
           |  |  | 111 |                   'query' => 'select device_id id,name name from device where device_id in (select device_id from device_device where removed is null and parent_id = ~~device_id~~) order by name',
 | 
        
           | 66 | rodolico | 112 |                   'noAdd' => true
 | 
        
           | 64 | rodolico | 113 |                   )
 | 
        
           | 59 | rodolico | 114 |             ) // children
 | 
        
           | 53 | rodolico | 115 |          ); // dbStructure
 | 
        
           |  |  | 116 |   | 
        
           | 72 | rodolico | 117 |   | 
        
           |  |  | 118 |       /**
 | 
        
           |  |  | 119 |        * Function to change the location of the current record
 | 
        
           |  |  | 120 |        * 
 | 
        
           |  |  | 121 |        * Mainly used by calling programs to change a location programmatically
 | 
        
           |  |  | 122 |        * instead of trying to push throuhg something else.
 | 
        
           |  |  | 123 |        * 
 | 
        
           |  |  | 124 |        * @parameter int $newLocation The index (location_id) to be used
 | 
        
           |  |  | 125 |        * @parameter string $effectiveDate Date in YYYYMMDD format. If null, uses now()
 | 
        
           |  |  | 126 |        */
 | 
        
           |  |  | 127 |       public function changeLocation( $newLocation, $effectiveDate = null ) {
 | 
        
           |  |  | 128 |          global $dbConnection;
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |          $effectiveDate = $effectiveDate ? "'$effectiveDate'" : 'now()';
 | 
        
           |  |  | 131 |          $query = sprintf( 
 | 
        
           |  |  | 132 |             'update %s set removed = %s where removed is null',
 | 
        
           |  |  | 133 |             $self::$dbStructure['table']['fields']['location']['linkageTable'],
 | 
        
           |  |  | 134 |             $effectiveDate
 | 
        
           |  |  | 135 |          );
 | 
        
           |  |  | 136 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 137 |          $query = sprintf(
 | 
        
           |  |  | 138 |             'insert into %s ( %s, %s, %s ) values (%s, %s, %s)',
 | 
        
           |  |  | 139 |             $self::$dbStructure['table']['fields']['location']['linkageTable'],
 | 
        
           |  |  | 140 |             $self::$dbStructure['table']['fields']['location']['foreignColumn'],
 | 
        
           |  |  | 141 |             $self::$dbStructure['table']['fields']['location']['linkageColumn'],
 | 
        
           |  |  | 142 |             $self::$dbStructure['table']['fields']['location']['addedField'],
 | 
        
           |  |  | 143 |             $newLocation,
 | 
        
           |  |  | 144 |             $this->id,
 | 
        
           |  |  | 145 |             $effectiveDate
 | 
        
           |  |  | 146 |          );
 | 
        
           |  |  | 147 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 148 |       } // changeLocation
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 |       /**
 | 
        
           |  |  | 151 |        * Function to change the owner of the current record
 | 
        
           |  |  | 152 |        * 
 | 
        
           |  |  | 153 |        * Mainly used by calling programs to change a owner programmatically
 | 
        
           |  |  | 154 |        * instead of trying to push throuhg something else.
 | 
        
           |  |  | 155 |        * 
 | 
        
           |  |  | 156 |        * @parameter int $newowner The index (owner_id) to be used
 | 
        
           |  |  | 157 |        * @parameter string $effectiveDate Date in YYYYMMDD format. If null, uses now()
 | 
        
           |  |  | 158 |        */
 | 
        
           |  |  | 159 |       public function changeOwner( $newOwner, $effectiveDate = null ) {
 | 
        
           |  |  | 160 |          global $dbConnection;
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 |          $effectiveDate = $effectiveDate ? "'$effectiveDate'" : 'now()';
 | 
        
           |  |  | 163 |          $query = sprintf( 
 | 
        
           |  |  | 164 |             'update %s set removed = %s where removed is null',
 | 
        
           |  |  | 165 |             $self::$dbStructure['table']['fields']['owner']['linkageTable'],
 | 
        
           |  |  | 166 |             $effectiveDate
 | 
        
           |  |  | 167 |          );
 | 
        
           |  |  | 168 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 169 |          $query = sprintf(
 | 
        
           |  |  | 170 |             'insert into %s ( %s, %s, %s ) values (%s, %s, %s)',
 | 
        
           |  |  | 171 |             $self::$dbStructure['table']['fields']['owner']['linkageTable'],
 | 
        
           |  |  | 172 |             $self::$dbStructure['table']['fields']['owner']['foreignColumn'],
 | 
        
           |  |  | 173 |             $self::$dbStructure['table']['fields']['owner']['linkageColumn'],
 | 
        
           |  |  | 174 |             $self::$dbStructure['table']['fields']['owner']['addedField'],
 | 
        
           |  |  | 175 |             $newOwner,
 | 
        
           |  |  | 176 |             $this->id,
 | 
        
           |  |  | 177 |             $effectiveDate
 | 
        
           |  |  | 178 |          );
 | 
        
           |  |  | 179 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 180 |       } // changeOwner
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 |       /**
 | 
        
           |  |  | 183 |        * Function to change the parent of the current record
 | 
        
           |  |  | 184 |        * 
 | 
        
           |  |  | 185 |        * Mainly used by calling programs to change a parent programmatically
 | 
        
           |  |  | 186 |        * instead of trying to push through something else.
 | 
        
           |  |  | 187 |        * 
 | 
        
           |  |  | 188 |        * @parameter int $newParent The index (parent_id) to be used
 | 
        
           |  |  | 189 |        * @parameter string $effectiveDate Date in YYYYMMDD format. If null, uses now()
 | 
        
           |  |  | 190 |        */
 | 
        
           |  |  | 191 |       public function changeParent( $newParent, $effectiveDate = null ) {
 | 
        
           |  |  | 192 |          global $dbConnection;
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |          $effectiveDate = $effectiveDate ? "'$effectiveDate'" : 'now()';
 | 
        
           |  |  | 195 |          $query = sprintf( 
 | 
        
           |  |  | 196 |             'update %s set removed = %s where removed is null',
 | 
        
           |  |  | 197 |             $self::$dbStructure['table']['fields']['parent']['linkageTable'],
 | 
        
           |  |  | 198 |             $effectiveDate
 | 
        
           |  |  | 199 |          );
 | 
        
           |  |  | 200 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 201 |          $query = sprintf(
 | 
        
           |  |  | 202 |             'insert into %s ( %s, %s, %s ) values (%s, %s, %s)',
 | 
        
           |  |  | 203 |             $self::$dbStructure['table']['fields']['parent']['linkageTable'],
 | 
        
           |  |  | 204 |             $self::$dbStructure['table']['fields']['parent']['foreignColumn'],
 | 
        
           |  |  | 205 |             $self::$dbStructure['table']['fields']['parent']['linkageColumn'],
 | 
        
           |  |  | 206 |             $self::$dbStructure['table']['fields']['parent']['addedField'],
 | 
        
           |  |  | 207 |             $newParent,
 | 
        
           |  |  | 208 |             $this->id,
 | 
        
           |  |  | 209 |             $effectiveDate
 | 
        
           |  |  | 210 |          );
 | 
        
           |  |  | 211 |          $dbConnection->doSQL( $query );
 | 
        
           |  |  | 212 |       } // changeParent
 | 
        
           |  |  | 213 |   | 
        
           | 59 | rodolico | 214 |    } // class Device
 | 
        
           | 53 | rodolico | 215 | ?>
 |