| 1 | rodolico | 1 | <?php
 | 
        
           | 38 | rodolico | 2 |    /*
 | 
        
           |  |  | 3 |     * Revision History:
 | 
        
           |  |  | 4 |     * 20161217 RWR
 | 
        
           |  |  | 5 |     * Added cleanLineReturn, removeBlanLines and getDOMUDOM0
 | 
        
           |  |  | 6 |     */
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 |    include_once( './maintenance_database.php' );
 | 
        
           | 52 | rodolico | 9 |    include_once( './database.php' );
 | 
        
           | 38 | rodolico | 10 |   | 
        
           |  |  | 11 |    /* function will take all eoln ("\n") and convert to <br /> if $makeHTML
 | 
        
           |  |  | 12 |     * is true, otherwise will replace all <br /> with \n.
 | 
        
           |  |  | 13 |     * Will then remove duplicates IF THEY ARE EXACTLY NEXT TO EACH OTHER
 | 
        
           |  |  | 14 |     * ie, with no intervening spaces.
 | 
        
           |  |  | 15 |     */
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 |    function cleanLineReturn ( $subject, $makeHTML = false ) {
 | 
        
           |  |  | 18 |       if ( $makeHTML ) {
 | 
        
           |  |  | 19 |          $search = "\n";
 | 
        
           |  |  | 20 |          $replace = "<br />";
 | 
        
           |  |  | 21 |       } else {
 | 
        
           |  |  | 22 |          $search = "<br />";
 | 
        
           |  |  | 23 |          $replace = "\n";
 | 
        
           |  |  | 24 |       }
 | 
        
           |  |  | 25 |       $subject = str_replace ( $search, $replace, $subject );
 | 
        
           |  |  | 26 |       return str_replace ( $replace . $replace, $replace, $subject );
 | 
        
           |  |  | 27 |    } // cleanLineReturn
 | 
        
           | 1 | rodolico | 28 |   | 
        
           | 38 | rodolico | 29 |   | 
        
           |  |  | 30 |    function removeBlankLines( $subject, $eoln = "\n" ) {
 | 
        
           |  |  | 31 |       return preg_replace( '/^[ \t]*[\r\n]+/m', '', $subject );
 | 
        
           |  |  | 32 |    }
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |   | 
        
           | 1 | rodolico | 35 |    function postMaintenance($device = '', $task = '', $notes = '', $technician = '', $date = '') {
 | 
        
           |  |  | 36 |       if ( $device ) { # parameters are being passed directly in
 | 
        
           |  |  | 37 |          $sql = 'insert into maintenance_performed (device_id,maintenance_task_id,maintenance_date,notes,login_id) values (' .
 | 
        
           |  |  | 38 |                implode( ',', 
 | 
        
           |  |  | 39 |                   array(
 | 
        
           |  |  | 40 |                      makeSafeSQLValue($device, 'I'), 
 | 
        
           |  |  | 41 |                      makeSafeSQLValue( $task, 'I'), 
 | 
        
           |  |  | 42 |                      makeSafeSQLValue($date, 'I'), 
 | 
        
           |  |  | 43 |                      makeSafeSQLValue($notes), 
 | 
        
           |  |  | 44 |                      makeSafeSQLValue($technician, 'I')
 | 
        
           |  |  | 45 |                   )
 | 
        
           |  |  | 46 |                ) . ')';
 | 
        
           |  |  | 47 |          queryDatabaseExtended( $sql );
 | 
        
           |  |  | 48 |          //print "<pre>$sql</pre>";
 | 
        
           |  |  | 49 |       } else {
 | 
        
           |  |  | 50 |          foreach ( $_POST as $parameter => $value ) {
 | 
        
           |  |  | 51 |             if ( preg_match( '/performed_(\d+)/', $parameter, $id ) ) { // this is the checkbox
 | 
        
           |  |  | 52 |                if ( $value ) { // and it has a value
 | 
        
           |  |  | 53 |                   $id = $id[1];
 | 
        
           |  |  | 54 |                   $sql = array( 'select device_id', 'maintenance_task_id',$_POST["datedone_$id"],makeSafeSQLValue($_POST["notes_$id"],'S'),$_POST["technician_$id"]);
 | 
        
           |  |  | 55 |                   $sql = implode (',', $sql );
 | 
        
           |  |  | 56 |                   $sql = 'insert into maintenance_performed (device_id,maintenance_task_id,maintenance_date,notes,login_id) ' . $sql . 
 | 
        
           |  |  | 57 |                         " from maintenance_schedule where maintenance_schedule_id = $id";
 | 
        
           |  |  | 58 |                   queryDatabaseExtended( $sql );
 | 
        
           |  |  | 59 |                   //print "<pre>$sql</pre>";
 | 
        
           |  |  | 60 |                }
 | 
        
           |  |  | 61 |             }
 | 
        
           |  |  | 62 |          }
 | 
        
           |  |  | 63 |       }
 | 
        
           |  |  | 64 |    }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |    function getSoftwareVersions ( $currentOnly = true, $softwareID = '',$computerID='' ) {
 | 
        
           |  |  | 67 |       $whereClause = array();
 | 
        
           |  |  | 68 |       if ( $currentOnly ) {
 | 
        
           |  |  | 69 |          $whereClause[] = 'installed_packages.removed_date is null';
 | 
        
           |  |  | 70 |       }
 | 
        
           |  |  | 71 |       if ( $softwareID ) {
 | 
        
           |  |  | 72 |          $whereClause[] = "package_name like '%$softwareID%'";
 | 
        
           |  |  | 73 |       }
 | 
        
           |  |  | 74 |       print '<pre>' . implode( ' and ', $whereClause ) . '</pre>';
 | 
        
           |  |  | 75 |      return array($sql, implode ( ' and ', $whereClause ), ' order by ');
 | 
        
           |  |  | 76 |    }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |    function EditMaintenanceRecord( $id ) {
 | 
        
           |  |  | 79 |       global $DATABASE_DEFINITION;
 | 
        
           | 52 | rodolico | 80 |       print "<pre>\nInside EditMaintenanceRecord id = $id\n</pre>";
 | 
        
           | 1 | rodolico | 81 |       print $id == -1 ? addData( $DATABASE_DEFINITION['maintenance_schedule'] ) : editData( $DATABASE_DEFINITION['maintenance_schedule'], $id );
 | 
        
           |  |  | 82 |    }
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |    function postChanges( $id = '' ) {
 | 
        
           |  |  | 85 |       global $DATABASE_DEFINITION;
 | 
        
           |  |  | 86 |       if ($id) {
 | 
        
           |  |  | 87 |          updateData( $DATABASE_DEFINITION['maintenance_schedule'], $id );
 | 
        
           |  |  | 88 |       } else {
 | 
        
           |  |  | 89 |          $result = insertData( $DATABASE_DEFINITION['maintenance_schedule'] );
 | 
        
           |  |  | 90 |          // There MUST be a record in maintenance_performed or this is not visible
 | 
        
           |  |  | 91 |          $id = $result['insert_id'];
 | 
        
           |  |  | 92 |          $sql = "insert into maintenance_performed (device_id,maintenance_task_id,maintenance_date,notes,login_id)
 | 
        
           |  |  | 93 |                   select device_id,maintenance_task_id,added_date,'Added to System',login_id 
 | 
        
           |  |  | 94 |                   from maintenance_schedule 
 | 
        
           |  |  | 95 |                   where maintenance_schedule_id = $id";
 | 
        
           |  |  | 96 |          doSQL($sql);
 | 
        
           |  |  | 97 |       }
 | 
        
           |  |  | 98 |    }
 | 
        
           | 38 | rodolico | 99 |   | 
        
           |  |  | 100 |    /*
 | 
        
           |  |  | 101 |     * Determine if the machine is a DOMU or a DOM0 (it can not be both, obviously)
 | 
        
           |  |  | 102 |     * If it is a DOMU, return the DOM0 name
 | 
        
           |  |  | 103 |     * If it is a DOM0, return an array containing all DOMU's on it
 | 
        
           |  |  | 104 |     */
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |    function getDOMUDOM0 ( $id = '' ) {
 | 
        
           |  |  | 107 |       if ( $id ) {
 | 
        
           |  |  | 108 |          // first, get See if it is a DOMU (ie, it has a "part_of" that is not null)
 | 
        
           |  |  | 109 |          $query = "select
 | 
        
           |  |  | 110 |                      DOM0.device_id id,
 | 
        
           |  |  | 111 |                      DOM0.name      name
 | 
        
           |  |  | 112 |                   from 
 | 
        
           |  |  | 113 |                      device DOMU join device DOM0
 | 
        
           |  |  | 114 |                   where
 | 
        
           |  |  | 115 |                      DOMU.part_of = DOM0.device_id
 | 
        
           |  |  | 116 |                      and DOMU.removed_date is null
 | 
        
           |  |  | 117 |                      and DOMU.device_id = $id";
 | 
        
           |  |  | 118 |          $results = queryDatabaseExtended(  $query );
 | 
        
           |  |  | 119 |          if ( $results['count'] == 1 ) { // can not have more than 1
 | 
        
           |  |  | 120 |             return array( 
 | 
        
           |  |  | 121 |                           'name' => $results['data'][0]['name'],
 | 
        
           |  |  | 122 |                           'id'   => $results['data'][0]['id']
 | 
        
           |  |  | 123 |                         );
 | 
        
           |  |  | 124 |          } else {
 | 
        
           |  |  | 125 |             // see if it is a DOM0
 | 
        
           |  |  | 126 |             $query = "select 
 | 
        
           |  |  | 127 |                         device.name      name,
 | 
        
           |  |  | 128 |                         device.device_id id
 | 
        
           |  |  | 129 |                      from
 | 
        
           |  |  | 130 |                         device join device_type using (device_type_id)
 | 
        
           |  |  | 131 |                      where
 | 
        
           |  |  | 132 |                         device.removed_date is null
 | 
        
           |  |  | 133 |                         and device_type.show_as_system = 'Y'
 | 
        
           |  |  | 134 |                         and device.part_of = $id";
 | 
        
           |  |  | 135 |             $results = queryDatabaseExtended(  $query );
 | 
        
           |  |  | 136 |             if ( $results['count'] ) {
 | 
        
           |  |  | 137 |                $returnValue = array();
 | 
        
           |  |  | 138 |                $results = $results['data'];
 | 
        
           |  |  | 139 |                foreach ( $results as $entry ) {
 | 
        
           |  |  | 140 |                   $returnValue[] = array( 'name' => $entry['name'],
 | 
        
           |  |  | 141 |                                             'id' => $entry['id']
 | 
        
           |  |  | 142 |                                            );
 | 
        
           |  |  | 143 |                }
 | 
        
           |  |  | 144 |                return $returnValue;
 | 
        
           |  |  | 145 |             } // if we are a dom0
 | 
        
           |  |  | 146 |          } // if..else
 | 
        
           |  |  | 147 |       } // if id
 | 
        
           |  |  | 148 |       return null;
 | 
        
           |  |  | 149 |    } // function getDOMUDOM0
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |   | 
        
           | 1 | rodolico | 152 |   | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |   | 
        
           | 38 | rodolico | 155 | ?>
 |