(C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011 (C) OpenEyes Foundation, 2011-2013 This file is part of OpenEyes. OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see .
Author: OpenEyes (info@openeyes.org.uk)
Inheritance: extends BaseActiveRecordVersioned
    public static function getXmlDetails( $xmlFile )
    {
        $data = array();
        $remoteIds = array();
        $articleIds = array();

        $sql = "SELECT * FROM import_status s WHERE s.xml_file='{$xmlFile}' ORDER BY s.version DESC";
        $db = self::db();
        $rows = $db->arrayQuery( $sql );

        foreach ( $rows as $row )
        {
            $data[$row['id']] = $row;
            if ( !empty( $row['remote_id'] ) )
            {
                if ( !isset( $remoteIds[$row['remote_id']] ) )
                {
                    $remoteIds[$row['remote_id']] = array();
                }
                $remoteIds[$row['remote_id']][] = $row['id'];
            }

            if ( !empty( $row['article_id'] ) )
            {
                if ( !isset( $articleIds[$row['article_id']] ) )
                {
                    $articleIds[$row['article_id']] = array();
                }
                $articleIds[$row['article_id']][] = $row['id'];
            }
        }

        if ( !empty( $remoteIds ) )
        {
            $rows = ImportStatus::fetchObjectAndNodeIds( array_keys( $remoteIds ) );
            foreach( $rows as $row )
            {
                foreach( $remoteIds[$row['remote_id']] as $id )
                {
                    $data[$id]['node_id'] = $row['main_node_id'];
                    $data[$id]['object_id'] = $row['id'];
                }
            }
        }

        if ( !empty( $articleIds ) )
        {
            $rows = ImportStatus::fetchArticleDetails( array_keys( $articleIds ) );
            foreach ( $rows as $row )
            {
                foreach( $articleIds[$row['id']] as $id )
                {
                    unset( $row['id'] );
                    $data[$id] = array_merge( $data[$id], $row );
                }
            }
        }

        return $data;
    }
    public function processXmlFiles()
    {
        $xmlNames = array();
        $remoteIds = array();

        foreach ( $this->xmlFiles as $key => $xmlFile )
        {
            $xmlNames[$key] = $xmlFile['name'];
        }

        if ( !empty( $xmlNames ) )
        {
            $sql = "SELECT * FROM import_status s WHERE s.xml_file IN ('" . implode( "','" , $xmlNames ) . "') ORDER BY s.xml_file, s.version DESC";
            $db = MMImportMonitorHelper::db();
            $rows = $db->arrayQuery( $sql );

            $keyMap = array_flip( $xmlNames );
            foreach ( $rows as $row )
            {
                $key = $keyMap[$row['xml_file']];
                if ( !isset( $this->xmlFiles[$key] ) )
                {
                    $this->xmlFiles[$key] = array(
                        'status' => array()
                    );
                }
                $this->xmlFiles[$key]['status'][] = $row;

                if ( !empty( $row['remote_id'] ) )
                {
                    if ( !isset( $remoteIds[$row['remote_id']] ) )
                    {
                        $remoteIds[$row['remote_id']] = array();
                    }
                    $remoteIds[$row['remote_id']][] = array(
                            'key' => $key,
                            'index' => count( $this->xmlFiles[$key]['status'] ) - 1
                    );
                }
            }

            $ids = ImportStatus::fetchObjectAndNodeIds( array_keys( $remoteIds ) );
            foreach ( $ids as $id )
            {
                foreach ( $remoteIds[$id['remote_id']] as $target )
                {
                    $this->xmlFiles[$target['key']]['status'][$target['index']]['object_id'] = $id['id'];
                    $this->xmlFiles[$target['key']]['status'][$target['index']]['node_id'] = $id['main_node_id'];
                }
            }
        }


        $this->isXmlProcessed = true;
    }
<?php

$http = eZHTTPTool::instance();
$rows = ImportStatus::getForLogList( ImportStatus::MODE_FETCH, $_POST, $http->postVariable( 'limit', 25 ), $http->postVariable( 'offset', 0 ) );
$totalCount = ImportStatus::getForLogList( ImportStatus::MODE_COUNT, $_POST );

$response = array(
    'content' => array(
        'records' => array(),
        'totalRecords' => $totalCount,
        'startIndex' => max( 0, min( $http->postVariable( 'offset' ), $totalCount - 1 ) )
    )
);

foreach ( $rows as $row )
{
    $date = !empty( $row['date'] ) ? date( 'Y-m-d', $row['date'] ) : '';
    $response['content']['records'][] = array(
        'id' => $row['id'],
        'status_xml_mdd' => $row['status_xml_mdd'],
        'status_mdd_ez' => $row['status_mdd_ez'],
        'version' => $row['version'],
        'import_date' => $date,
        'xml_filename' => $row['xml_file'],
        'error_messages' => $row['error_messages'],
        'warning_messages' => $row['warning_messages'],
        'remote_id' => $row['remote_id'],
        'object_id' => $row['object_id'],
        'node_id' => $row['node_id']
    );
}
    case 'clean':
    {
        $pid = (integer)$Params['Param1'];
        if ( !empty( $pid ) )
        {
            $shellScript = $ini->variable( 'ImportMonitor' , 'ShellScript' );
            exec( "{$shellScript} clean {$pid}" );
            return $module->redirectTo( "/importmonitor/lock" );
        }
        break;
    }

    case 'reimportxml':
    {
        $xmlFiles = ImportStatus::getXmlFilenames( $http->postVariable( 'xml', array() ) );
        MMImportMonotorArchive::reimportXml( $xmlFiles );
        break;
    }

    case 'reimportarchive':
    {
        $context = $http->postVariable( 'context', false );
        $archiveFilename = $http->postVariable( 'archive', false );
        MMImportMonotorArchive::reimportArchive( $context, $archiveFilename );
        return $module->redirectTo( "/importmonitor/archive/(context)/{$context}/(name)/{$archiveFilename}" );
        break;
    }
}

return eZExecution::cleanExit();
 /**
  * @param $eventQuery
  * @param $opNoteEvent
  * @param $creator
  * @param $portalUserId
  * @param $examination
  * @param $eventType
  * @param $eyeIds
  * @param $refractionType
  *
  * @throws CHttpException
  */
 protected function createExamination($eventQuery, $episodeId, $creator, $portalUserId, $examination, $eventType, $eyeIds, $refractionType, $opNoteId = null)
 {
     $transaction = $eventQuery->getDbConnection()->beginInternalTransaction();
     try {
         $examinationEvent = $creator->saveExamination($episodeId, $portalUserId, $examination, $eventType, $eyeIds, $refractionType, $opNoteId);
         if ($eventQuery->event) {
             //delete old event
             $eventQuery->event->deleted = 1;
             $eventQuery->event->save();
         }
         //update log for new event
         $eventQuery->import_success = ImportStatus::model()->find('status_value = "Success Event"')->id;
         $eventQuery->event_id = $examinationEvent->id;
         $eventQuery->save();
     } catch (Exception $e) {
         $transaction->rollback();
         throw new CHttpException(500, 'Saving Examination event failed');
     }
     $transaction->commit();
 }