public function searchAction()
 {
     $fid = $this->getSanParam('fid');
     // check for file
     if (empty($fid)) {
         $this->status->setStatusMessage(t('Please upload a file to continue'));
         $this->_redirect('sync/upload');
     }
     // status dump for async
     if ($this->getSanParam('statuscheck')) {
         $syncLog = new SyncLog($fid);
         $remaining = count(SyncCompare::$compareTypes) - $syncLog->getDiffStatus();
         $this->sendData(array($remaining));
         return;
     } else {
         if ($this->getSanParam('startjob')) {
             // thread start for async
             try {
                 $syncCompare = new SyncCompare($fid);
                 $msg = $syncCompare->sanityCheck();
                 if (!$msg) {
                     $has_errors = $syncCompare->findDifferencesProcess();
                 } else {
                     $has_errors = $msg;
                 }
             } catch (Exception $e) {
                 $has_errors = $e->getMessage();
             }
             if ($has_errors) {
                 $this->sendData(array("Done with errors \n" . print_r($has_errors, true)));
             } else {
                 $this->sendData(array('Done.'));
             }
             return;
         }
     }
     $this->view->assign('fid', $fid);
 }
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);
}