Subversion Repositories computer_asset_manager_v1

Rev

Rev 3 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 36
Line 2... Line 2...
2
 
2
 
3
/*
3
/*
4
 * these are some global functions which we need since their classes have not
4
 * these are some global functions which we need since their classes have not
5
 * been created yet. The code that calls these should be modified once CAMPv2
5
 * been created yet. The code that calls these should be modified once CAMPv2
6
 * is complete. At that point, these should be calls to those classes.
6
 * is complete. At that point, these should be calls to those classes.
-
 
7
 * 
-
 
8
 * 20160914 RWR
-
 
9
 * 
-
 
10
 * Modified fuzzyFindMachineID to look for device based solely on device name
-
 
11
 * if client name is not included.
-
 
12
 * 
-
 
13
 * 20161023 RWR
-
 
14
 * 
-
 
15
 * Removed fuzzyFindMachineID and changed getMachineID to use one single query
-
 
16
 * 
7
 */
17
 */
8
 
18
 
9
   function makeSafeSQLValue ( $value, $type='S' ) {
19
   function makeSafeSQLValue ( $value, $type='S' ) {
10
      if(get_magic_quotes_gpc()) {
20
      if(get_magic_quotes_gpc()) {
11
           $value = stripslashes($value);
21
           $value = stripslashes($value);
Line 97... Line 107...
97
   // or null no value returned
107
   // or null no value returned
98
   function getOneDBValue( $sql ) {
108
   function getOneDBValue( $sql ) {
99
      $data = queryDatabaseExtended( $sql, false ); // get the query results into a standard array
109
      $data = queryDatabaseExtended( $sql, false ); // get the query results into a standard array
100
      return isset( $data['count'] ) ? $data['data'][0][0] : null;
110
      return isset( $data['count'] ) ? $data['data'][0][0] : null;
101
   }
111
   }
-
 
112
   
-
 
113
   function getOneUniqueDBValue ( $sql ) {
-
 
114
      $data = queryDatabaseExtended( $sql, false ); // get the query results into a standard array
-
 
115
      if ( isset( $data['count'] ) and $data['count'] > 1 ) {
-
 
116
         throw new Exception ('Duplicate Values in Database' );
-
 
117
      }
-
 
118
      return isset( $data['count'] ) ? $data['data'][0][0] : null;
-
 
119
   }
-
 
120
      
102
  
121
  
103
  function countNumberOfRows ( $sql ) {
122
  function countNumberOfRows ( $sql ) {
104
     $count = queryDatabaseExtended("select count(*) numRows from ($sql) test");
123
     $count = queryDatabaseExtended("select count(*) numRows from ($sql) test");
105
     return $count['data'][0]['numRows'];
124
     return $count['data'][0]['numRows'];
106
  }
125
  }
Line 124... Line 143...
124
             select client_id from client_alias where alias = '$name' and removed_date is null");
143
             select client_id from client_alias where alias = '$name' and removed_date is null");
125
   return $client;
144
   return $client;
126
}
145
}
127
 
146
 
128
 
147
 
129
function addSerialNumber( $machineID, $serialNumber ) {
-
 
130
   queryDatabaseExtended( "update device set serial = '$serialNumber' where device_id = $machineID" );
-
 
131
}
-
 
132
 
-
 
133
function fuzzyFindMachineID ( $name, $clientID ) {
-
 
134
   return getOneDBValue ("select device_id
-
 
135
                from device join site using (site_id)
-
 
136
                where site.client_id = $clientID
-
 
137
                     and device.name = '$name'
-
 
138
                     and device.removed_date is null
-
 
139
               union
-
 
140
               select device_id 
-
 
141
               from device_alias join device using (device_id) join site using (site_id) 
-
 
142
               where device_alias.alias = '$name' and site.client_id = $clientID");
-
 
143
} // fuzzyFindMachineID
-
 
144
   
-
 
145
 
-
 
146
function getMachineID ( $name, $clientID, $serialNumber ) {
148
function getMachineID ( $name, $clientID = null, $serialNumber = null ) {
147
   $machineID = null;
-
 
148
   if ( isset( $serialNumber ) ) {
149
   if ( $serialNumber ) {
149
      $machineID = getOneDBValue( "select device_id from device where serial = '$serialNumber' and device.removed_date is null" );
150
      $query = "select device_id from device where serial = '$serialNumber'";
150
      if ( $machineID ) { # check if it is unique
-
 
151
         $count = getOneDBValue( "select count(*) from device where serial = '$serialNumber' and device.removed_date is null" );
-
 
152
         if ( $count > 1 ) { # woops, we have a duplicate serial number
-
 
153
            return -1;
-
 
154
         }
151
      try {
-
 
152
         $machineID = getOneUniqueDBValue( $query );
155
      }
153
      }
-
 
154
      catch ( Exception $e ) { // we have too many with the same serial number
-
 
155
         $machineID = null;
-
 
156
      }
-
 
157
   }
156
      if ( ! $machineID ) {
158
   if ( ! isset( $machineID ) ) {
-
 
159
      $whereClause[] = "(device.name = '$name' or alias.alias = '$name') and device.removed_date is null";
157
         if ( $machineID = fuzzyFindMachineID( $name, $clientID ) )
160
      if ( $clientID )     $whereClause[] = "site.client_id = $clientID";
-
 
161
      $query = 'select device_id from device left outer join alias using (device_id) join site using (site_id) where ' . implode( ' and ', $whereClause );
-
 
162
      try {
158
            addSerialNumber( $machineID, $serialNumber );
163
         $machineID = getOneUniqueDBValue( $query );
-
 
164
      }
-
 
165
      catch ( Exception $e ) {
-
 
166
         throw $e;
159
      }
167
      }
160
   }
168
   }
161
 
-
 
162
   if ( ! isset( $machineID ) )
-
 
163
      $machineID = getOneDBValue ("select device_id
-
 
164
                from device join site using (site_id)
-
 
165
                where site.client_id = $clientID
-
 
166
                     and device.name = '$name'
-
 
167
                     and device.removed_date is null
-
 
168
               union
-
 
169
               select device_id 
-
 
170
               from device_alias join device using (device_id) join site using (site_id) 
-
 
171
               where device_alias.alias = '$name' and site.client_id = $clientID");
-
 
172
   return $machineID;
169
   return $machineID;
173
}
170
}
174
 
171
 
175
?>
172
?>