public function uploadAction()
 {
     $user = Zend_Auth::getInstance()->getIdentity();
     $result = false;
     // move upload file
     if ($this->getRequest()->isPost() && !empty($_FILES)) {
         // check extension
         $name = $_FILES['upload']['name'];
         if (strpos(strrev($name), strrev('.sqlite')) !== 0) {
             $this->status->setStatusMessage(t('There was a problem with file types <code>' . $name . '</code>'));
             return;
         }
         $name = implode('.', array($user->id, $user->last_name, $user->first_name, date('Y-m-d'), $_FILES['upload']['name']));
         $path = rtrim(Globals::$BASE_PATH, '/') . '/files_sync/';
         if (!file_exists($path) && !mkdir($path, 0777, true)) {
             // make storage dir
             $this->status->setStatusMessage(t('There was a problem creating <code>' . $path . '</code>'));
             return;
         }
         $result = move_uploaded_file($_FILES['upload']['tmp_name'], $path . $name);
         if (!$result) {
             $this->status->setStatusMessage(t('There was a problem moving files <code>' . $path . $name . '</code>'));
             return;
         }
     }
     // create entry for file
     if ($result) {
         // source info
         $db = SyncCompare::getDesktopConnectionParams('_app', $path . $name);
         try {
             $_app = new ITechTable($db);
             $_app = $_app->fetchAll()->current();
             //look for previous sync time
             $previous_files = new SyncFile();
             $previous = $previous_files->getAdapter()->query("SELECT * FROM syncfile WHERE application_id = '" . $_app->app_id . "' AND timestamp_completed IS NOT NULL ORDER BY timestamp_completed DESC LIMIT 1");
             $previous_timestamp = $_app->init_timestamp;
             //first time
             if ($previous) {
                 $previous = $previous->fetchAll();
                 if ($previous) {
                     $previous_timestamp = $previous[0]['timestamp_completed'];
                 }
             }
             $save = array('filename' => $name, 'filepath' => $path, 'application_id' => $_app->app_id, 'application_version' => 1, 'timestamp_last_sync' => $previous_timestamp);
             // dest info
             $syncFile = new SyncFile();
             $result = $syncFile->insert($save);
             if ($result) {
                 $newname = $result . '.' . $name;
                 $good = rename($path . $name, $path . $newname);
                 if ($good) {
                     $syncFile->update(array('filename' => $newname), 'id = ' . $result);
                 }
             }
             if (!$result) {
                 $msg = "Could not save file.";
                 $this->status->setStatusMessage($msg);
             } else {
                 $this->status->setStatusMessage(t('Sync database file uploaded.'));
                 $this->_redirect('sync/search/fid/' . $result);
                 // .'/startjob/1/outputType/text' search for db changes
             }
         } catch (Exception $e) {
             $msg = t('Invalid desktop database file');
         }
     }
 }
function test($filename)
{
    $db = SyncCompare::getDesktopConnectionParams('_app', Globals::$BASE_PATH . 'app/controllers/sync/test_dbs/' . $filename);
    $_app = new ITechTable($db);
    $_app = $_app->fetchAll()->current();
    $previous_files = new SyncFile();
    $previous = $previous_files->getAdapter()->query("SELECT * FROM syncfile WHERE application_id = '" . $_app->app_id . "' AND timestamp_completed IS NOT NULL ORDER BY timestamp_completed DESC LIMIT 1");
    $previous_timestamp = $_app->init_timestamp;
    //first time
    if ($previous) {
        $previous = $previous->fetchAll();
        if ($previous) {
            $previous_timestamp = $previous[0]['timestamp_completed'];
        }
    }
    $save = array('filename' => $filename, 'filepath' => Globals::$BASE_PATH . 'app/controllers/sync/test_dbs/', 'application_id' => $_app->app_id, 'application_version' => $_app->app_id, 'timestamp_last_sync' => $previous_timestamp);
    // dest info
    $syncFile = new SyncFile();
    $fid = $syncFile->insert($save);
    try {
        $syncCompare = new SyncCompare($fid);
        echo "sanity check\n";
        $msg = $syncCompare->sanityCheck();
        if (!$msg) {
            echo "find diffs\n";
            $has_errors = $syncCompare->findDifferencesProcess();
            $syncLog = new SyncLog($fid);
            $totals = $syncLog->pendingTotals();
            foreach ($totals as $tot) {
                echo $tot['item_type'] . '::' . $tot['action'] . '::' . $tot['cnt'] . "\n";
            }
            $pendingList = $syncLog->pendingList();
            //var_dump($pendingList);
            //insert
            var_dump($syncCompare->doUpdatesProcess());
            echo "verifying \n";
            //look for inserted values
            foreach ($pendingList as $p) {
                if ($p->action == 'insert') {
                    $set = SyncSetFactory::create($p->item_type, $db, $fid);
                    if ($p->left_id) {
                        $lo = $set->fetchLeftItemById($p->left_id);
                        if ($p->item_type != 'trainer') {
                            $ro = $set->fetchFieldMatch($lo);
                        } else {
                            $ro = $set->fetchRightItemByUuid($lo->uuid);
                        }
                        if (!$ro) {
                            echo $p->item_type . ' not found: ' . $p->left_id . "\n";
                        }
                    }
                }
            }
        } else {
            $has_errors = $msg;
        }
    } catch (Exception $e) {
        $has_errors = $e->getMessage();
    }
    try {
        if (!$msg) {
            echo "find diffs\n";
            //$has_errors = $syncCompare->findDifferencesProcess();
        } else {
            $has_errors = $msg;
        }
    } catch (Exception $e) {
        $has_errors = $e->getMessage();
    }
    var_dump($has_errors);
}