~chinthakarukshan/sahana-agasti/Self_Running_Installer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
<?php

/**
 * Agasti 2.0 Installer
 *
 * PHP Version 5
 *
 * LICENSE: This source file is subject to LGPLv3.0 license
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html
 *
 * @author Charles Wisniewski, CUNY SPS
 *
 * Copyright of the Sahana Software Foundation, sahanafoundation.org
 */
/**
 * Sahana Agasti 2.0 install.inc.php
 * this file houses installation specific functions, primarily called from install.php
 */
require_once (dirname(__FILE__) . '/sfYaml.php');
//require_once (dirname(__FILE__) . '/ProjectConfiguration.class.php');
require_once(dirname(__FILE__) . '/requirements.inc.php');
require_once (dirname(__FILE__) . '/func.inc.php');
require_once (dirname(__FILE__) . '/sfDatabaseManager.class.php');
//$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'all', false);

class agInstall
{
  /* protected *//*
    var $INS_CONFIG;
    var $DISABLE_NEXT;
    var $steps = array();
   */

  /* public */

  function __construct(&$INS_CONFIG)
  {
    $this->DISABLE_NEXT = FALSE;
    $this->RETRY_SUCCESS = FALSE;
    $this->ERROR_MESSAGE = '';
    $this->INSTALL_RESULT = '';

    $this->INS_CONFIG = &$INS_CONFIG;

    $this->steps = array(
      0 => array('title' => '1. Introduction', 'fun' => 'stage0'),
      1 => array('title' => '2. License Agreement', 'fun' => 'stage1'),
      2 => array('title' => '3. Prerequisite Check', 'fun' => 'stage2'),
      3 => array('title' => '4. Configure Database', 'fun' => 'stage3'),
      4 => array('title' => '5. Configuration Summary', 'fun' => 'stage4'),
      5 => array('title' => '6. Installation Summary', 'fun' => 'stage5'),
//    6 => array('title' => '7. Enter Secure Mode', 'fun' => 'stage6' ),
    );
    $this->EventHandler();
    GLOBAL $trans;
    $trans = array(
//     requirements.inc.php
      'S_PHP_VERSION' => 'PHP version',
      'S_MINIMAL_VERSION_OF_PHP_IS' => 'Minimal version of PHP is',
      'S_PHP_MEMORY_LIMIT' => 'PHP memory limit',
      'S_IS_A_MINIMAL_PHP_MEMORY_LIMITATION_SMALL' => 'is a minimal PHP memory limitation',
      'S_PHP_POST_MAX_SIZE' => 'PHP post max size',
      'S_IS_A_MINIMUM_SIZE_OF_PHP_POST_SMALL' => 'is minimum size of PHP post',
      'S_PHP_MAX_EXECUTION_TIME' => 'PHP max execution time',
      'S_PHP_MAX_INPUT_TIME' => 'PHP max input time',
      'S_IS_A_MINIMAL_LIMITATION_EXECTUTION_TIME_SMALL' => 'is a minimal limitation on execution time of PHP scripts',
      'S_IS_A_MINIMAL_LIMITATION_INPUT_PARSE_TIME_SMALL' => 'is a minimal limitation on input parse time for PHP scripts',
      'S_PHP_TIMEZONE' => 'PHP timezone',
      'S_NO_SMALL' => 'no',
      'S_YES_SMALL' => 'yes',
      'S_TIMEZONE_FOR_PHP_IS_NOT_SET' => 'Timezone for PHP is not set',
      'S_PLEASE_SET' => 'Please set',
      'S_OPTION_IN_SMALL' => 'option in',
      'S_PHP_DATABASES_SUPPORT' => 'PHP databases support',
      'S_REQUIRES_ANY_DATABASE_SUPPORT' => 'Requires any database support [MySQL or PostgreSQL or Oracle or SQLite3]',
      'S_REQUIRES_BCMATH_MODULE' => 'Requires bcmath module',
      'S_CONFIGURE_PHP_WITH_SMALL' => 'configure PHP with',
      'S_REQUIRES_MB_STRING_MODULE' => 'Requires mb string module',
      'S_PHP_SOCKETS' => 'PHP Sockets',
      'S_REQUIRED_SOCKETS_MODULE' => 'Required Sockets module',
      'S_THE_GD_EXTENSION_IS_NOT_LOADED' => 'The GD extension is not loaded.',
      'S_GD_PNG_SUPPORT' => 'GD PNG Support',
      'S_REQUIRES_IMAGES_GENERATION_SUPPORT' => 'Requires images generation support',
      'S_LIBXML_MODULE' => 'libxml module',
      'S_PHPXML_MODULE_IS_NOT_INSTALLED' => 'php-xml module is not installed',
      'S_CTYPE_MODULE' => 'ctype module',
      'S_REQUIRES_CTYPE_MODULE' => 'Requires ctype module',
      'S_PHP_UPLOAD_MAX_FILESIZE' => 'PHP upload max filesize',
      'S_IS_MINIMAL_FOR_PHP_ULOAD_FILESIZE_SMALL' => 'is minimum for PHP upload filesize',
      'S_SESSION_MODULE' => 'PHP Session',
      'S_REQUIRED_SESSION_MODULE' => 'Required Session module',
    );
    foreach ($trans as $const => $label) {
      if (!defined($const))
        define($const, $label);
    }
    unset($GLOBALS['trans']);
  }

  function getConfig($name, $default = null)
  {
//if entry method to this function is admin/config instead of install, set the global
    return isset($this->INS_CONFIG[$name]) ? $this->INS_CONFIG[$name] : $default;
  }

  function setConfig($name, $value)
  {
    return ($this->INS_CONFIG[$name] = $value);
  }

  function getStep()
  {
    return $this->getConfig('step', 0);
  }

  function DoNext()
  {
    if (isset($this->steps[$this->getStep() + 1])) {
      $this->INS_CONFIG['step']++;
      //echo "Doing the forward Step" .$this->AG_CONFIG['step'];
      return true;
    }
    return false;
  }

  function DoBack()
  {
    if (isset($this->steps[$this->getStep() - 1])) {
      $this->INS_CONFIG['step']--;
      //echo "Doing the backward Step" .$this->AG_CONFIG['step'];
      return true;
    }
    return false;
  }

  function getList()
  {
    /**
     * this is a simple html constructor, takes our array and
     * generates a series of list items
     */
    $list = "<ul>";
    foreach ($this->steps as $id => $data) {
      if ($id < $this->getStep())
        $style = 'completed';
      else if ($id == $this->getStep())
        $style = 'current';
      else
        $style = null;

      $list = $list . '<li class="' . $style . '">' . $data['title'] . '</li>';
    }
    $list = $list . "</ul>";
    return $list;
  }

  function getState()
  {
    $fun = $this->steps[$this->getStep()]['fun'];
    return $this->$fun();
  }

  function stage0()
  {
    return '<div class=info><h2>Welcome to the Sahana Agasti 2.0 Installation Wizard</h2><br />
      <p>Agasti is an emergency management application with tools to manage staff, resources, 
      client information and facilities through an easy to use web interface. The Installation
      Wizard will guide you through the installation of Agasti 2.0.</p> <br />
      Click the "Next" button to proceed to the next screen. If you want to change something at 
      a previous step, click the "Previous" button.  You may cancel installation at any time by
      clicking the "Cancel" button.</p>
      <p><b> Click the "Next" button to continue.</p></b></div>';
  }

  function stage1()
  {
    ///$LICENSE_FILE = sfConfig::get('sf_root_dir') . '/LICENSE';

      //$sfconfigarray=sfConfig::getAll();
      //echo $sfconfigarray[0];

    $LICENSE_FILE=LICENSE;
    $this->DISABLE_NEXT = !$this->getConfig('agree', false);

    $license = 'Missing licence file. See GPL licence.';
    if (file_exists($LICENSE_FILE))
      $license = file_get_contents($LICENSE_FILE);


    $agree = '<input class="checkbox" type="checkbox" value="yes" name="agree" id="agree" onclick="submit();"';
   $this->getConfig('agree', false) == 'yes' ? $agree .= ' checked=checked>' : $agree .= '>';
    //echo $agree;
   // if($this->getConfig('agree', false)==false)
      //          {
    //    echo "got it";
     //           }
     //           else
      //              echo "sorry";


    return '<div class=info>' . nl2br($license) . "</div><br />" . $agree . '<label for="agree">I agree</label>';
  }

  /**
   * step 2: pre-requisite check
   * @return block of html for UI of stage/step 2
   */
  function stage2()
  {
      //return "this is stage 2";
      //redirect('http://www.google.lk/');

    $table = '<table class="requirements" style="align:center; width:100%;">
                <th><tr style="font-weight:bold;">
                  <td>Option</td><td>Current Value</td><td>Required</td><td>Recommended</td><td>&nbsp;</td></tr></th>';

    
    /**
     * @todo clean up the following code
     */
    $final_result = true;

    $reqs = check_php_requirements();
    foreach ($reqs as $req) {

      $result = null;
      if (!is_null($req['recommended']) && ($req['result'] == 1)) {
        $result = '<span class="orange">Ok</span>';
      } else if ((!is_null($req['recommended']) && ($req['result'] == 2))
          || (is_null($req['recommended']) && ($req['result'] == 1))) {
        $result = '<span class="green">Ok</span>';
      } else if ($req['result'] == 0) {
        $result = '<span class="fail">Fail</span>';
// this will be useful$result->setHint($req['error']);
      }

      $current = '<tr><td><strong>' . $req['name'] . '</strong></td>' . '<td>' . $req['current'] . '</td>';
      $required = $req['required'] ? $req['required'] : '&nbsp;';
      $recommend = $req['recommended'] ? $req['recommended'] : '&nbsp;';
      $res = $req['result'] ? '&nbsp;' : 'fail';
      $row = '<td>' . $current . '</td><td>' . $required . '</td><td>' . $recommend . '</td><td>' . $result . '</td>';

      $table = $table . $row . '</tr>';

      $final_result &= (bool) $req['result'];
    }

    if (!$final_result) {
      $this->DISABLE_NEXT = true;

      $retry = '<input type="submit" class="inputGray" id="retry" name="retry" value="retry" />';
      $final_result = '<span class="fail">There are errors in your configuration.  
        Please correct all issues and press the "retry" button.  For additional assistance
        reference the README file.</span><br /><br />' . $retry;
    } else {
      $this->DISABLE_NEXT = false;
      $final_result = '<span class="green">Your system is properly configured.  Please continue.</span>';
    }

    return $table . '</table><br />' . $final_result;
  }

  function stage3()
  {
      //echo "Howdy to stage 2";
    global $INS_CONFIG;
    $this->getCurrent();

    if (isset($_REQUEST['retry']) && $this->RETRY_SUCCESS == false) {
      $retry_label = 'retry';
      $instruct = '<span class="fail">Error</span><br />
      <br />Error Message:' . $this->ERROR_MESSAGE . '<br /><span class="fail">Please correct the
        error and press retry.</span>';
    } else if ($this->RETRY_SUCCESS == false) {
      $retry_label = 'test connection';
      $instruct = 'Press "Test connection" button when done.';
    } else {
      $instruct = 'Database Settings are <span class="green">Ok</span> please click next';
    }
    $retry = '';
    if ($this->RETRY_SUCCESS == false) {
      $retry = '<input type="submit" class="linkButton" id="retry" name="retry" value="' . $retry_label . '" />';
    }
    $table = '<fieldset>
              <legend><img src="images/database.png" alt="database icon" />Database Configuration:</legend>

              <ul>
                <li>
                  <label>host:</label>
                  <input type="text" name="db_host" id="db_host" class="inputGray"
                         value="' . $this->getConfig('DB_SERVER', 'localhost') . '"/>
                </li>
                <li>
                  <label>database:</label>
                  <input type="text" name="db_name" id="db_name" class="inputGray"
                         value="' . $this->getConfig('DB_DATABASE', 'agasti23') . '" />
                </li>
                <li>
                  <label>username:</label>
                  <input type="text" name="db_user" id="db_user" class="inputGray"
                         value="' . $this->getConfig('DB_USER', 'root') . '" />
                </li>
                <li>
                  <label>password:</label>
                  <input type="password" name="db_pass" id="db_pass" class="inputGray"
                         value="' . $this->getConfig('DB_PASSWORD', 'root') . '" />
                </li>
                <input id="init_schema" type="hidden" name="init_schema" checked="checked" />
                <li><span class="fail">this will drop your current database.</span></li>
              </ul>
            </fieldset>
            <fieldset>
              <legend><img src="images/config.png" alt="config gear icon" />Administrator Information:</legend>
              <ul>
                <li>
                  <label>name:</label>
                  <input type="text" name="admin_name" id="admin_name" class="inputGray"
                         value="' . $this->getConfig('ADMIN_NAME', 'administrator') . '" /><br />
                </li>
                <li>
                  <label>email:</label>
                  <input type="text" name="admin_email" id="admin_email" class="inputGray"
                         value="' . $this->getConfig('ADMIN_EMAIL', 'b@m.an') . '" /><br />
                </li>
              </ul>
            </fieldset>';
    $results = 'The database is created manually.  First, the Agasti Installer will test your
      configuration settings before continuing.  Enter your database settings and click "Test Connection". <br /><br/>'
        . $instruct . $table . $retry;

//$this->DISABLE_NEXT ? new CSpan(S_OK,'ok') :  new CSpan(S_FAIL, 'fail'),
//new  CButton('retry', 'Test connection')
    return $results;
  }

  function stage4()
  {
    $current = $this->getCurrent();

    return 'Below is your installation configuration summary:<br /><div class="info">
        <strong>Database Host</strong>: ' . $this->getConfig('DB_SERVER') .
    '<br /><strong>Database Name</strong>: ' . $this->getConfig('DB_DATABASE') .
    '<br /><strong>Database User</strong>: ' . $this->getConfig('DB_USER') .
    '<br /><strong>Database Password</strong>: ' . preg_replace('/./', '*', $this->getConfig('DB_PASSWORD', 'unknown')) .
    '<br /><strong>Administrator</strong>: ' . $this->getConfig('ADMIN_NAME') .
    '<br /><strong>Admin E-mail</strong>: ' . $this->getConfig('ADMIN_EMAIL') .
    '</div><br /> Please verify your settings.  By clicking next you will install Sahana Agasti.';
  }

  function stage5()
  {
    if ($this->INSTALL_RESULT == 'Success!') {
      return '<span class="okay">Congratulations!  Installation was successful:</span> <br /><div class="info">
        <strong>Database Host</strong>: ' . $this->getConfig('DB_SERVER') .
      '<br /><strong>Database Name</strong>: ' . $this->getConfig('DB_DATABASE') .
      '<br /><strong>Database User</strong>: ' . $this->getConfig('DB_USER') .
      '<br /><strong>Database Password</strong>: ' . preg_replace('/./', '*', $this->getConfig('DB_PASSWORD', 'unknown')) .
      '<br /><strong>Administrator</strong>: ' . $this->getConfig('ADMIN_NAME') .
      '<br /><strong>Admin E-mail</strong>: ' . $this->getConfig('ADMIN_EMAIL') .
      '</div><br /> NOTE: to continue with Agasti setup you must first create the "Super User"
        account by editing the config file.  In .../config please edit the config.yml file with the
        Super User username and password.  After you have done so, click finish and you will be 
        redirected to log in with the Super User username and password and thenthen create your
        first user.';
    } else {
      return '<span class="fail">There was an error with your installation:</span><br /><div class="info">' . $this->INSTALL_RESULT . '</div>';
    }
  }

  function stage6()
  {
    return "there is no step 6 in the installer, well, no screen for it at least: this should login the user and redirect to admin/createuser, i.e. you shouldn't even SEE this";
  }

  function getCurrent()
  {
    $filename = 'databases.yml';
    if (file_exists($filename)) {
      $dbArray = sfYaml::load($filename);
    } else {
      $install_flag = false;
    }
    $filename = 'config.yml';
    if (file_exists($filename)) {
      $cfgArray = sfYaml::load($filename);
    } else {
      $install_flag = true;
      $existing_auth_method = "bypass";
    }
    try {
      $db_params = parseDSN($dbArray['all']['doctrine']['param']['dsn']);
      $this->setConfig('db_config', $dbArray);
      $this->setConfig('DB_SERVER', $db_params['host']);
      $this->setConfig('DB_DATABASE', $db_params['dbname']);
      $this->setConfig('DB_USER', $dbArray['all']['doctrine']['param']['username']);
      $this->setConfig('DB_PASSWORD', $dbArray['all']['doctrine']['param']['password']);
      $this->setConfig('ADMIN_NAME', $cfgArray['admin']['admin_name']);
      $this->setConfig('ADMIN_EMAIL', $cfgArray['admin']['admin_email']);
      $this->setConfig('AUTH_METHOD', $cfgArray['admin']['auth_method']['value']);
    } catch (Exception $e) {
      return 'file was unreadable';
    }
    return array($dbArray, $cfgArray);
  }

  function dbParams($db_params)
  {
    $arguments = array(
      'task' => 'configure:database',
      //'dsn' => buildDsnString('mysql', $_POST['db_host'], $_POST['db_name'], $_POST['db_port']),
      'dsn' => $db_params['dsn'], // ilya 2010-07-21 15:16:58
//'dsn' => buildDsnString($_POST['db_type'], $_POST['db_host'], $_POST['db_name'], $_POST['db_port']),
      'hostname'=>$db_params['hostname'],
      'dbname'=>$db_params['dbname'],
      'username' => $db_params['username'],
      'password' => $db_params['password'],
    );
    $options = array(
      'help' => null,
      'quiet' => null,
      'trace' => null,
      'version' => null,
      'color' => null,
      'env' => 'all',
      'name' => 'doctrine',
      'class' => 'sfDoctrineDatabase',
      'app' => null
    );
    //$dispatcher = new sfEventDispatcher();
    //$formatter = new sfFormatter();
    $dbConfig = new agDoctrineConfigureDatabaseTask();
    $dbConfig->execute($arguments, $options);
    try {
      //$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'all', false);
    } catch (Exception $e) {
      $configuration = false;
    }
    //return $configuration;
  }

  function CheckConnection($db_config)
  {
    if ($db_config['dsn'] != '') {
      try {
        $this->dbParams($db_config);
        //$databaseManager = new sfDatabaseManager($this->dbParams($db_config));
        //$connection = Doctrine_Manager::connection()->connect();
        $serverlink=mysql_connect($db_config['hostname'], $db_config['username'], $db_config['password']);
        if(!$serverlink)
            $result='Error in connecting to Server'.mysql_error();
        else{
            $dbcheck=mysql_select_db($db_config['dbname']);
            if(!$dbcheck){
                $result='Error in connecting to database'.mysql_error();
            }
            else
                $result = 'good';
        }
            
      } catch (Exception $e) {
        $result = $e->getMessage();
      }
    } else {
      $result = 'Database configuration is not valid: bad DSN';
    }
    return $result;
  }

  function doInstall($db_params)
  {
    $databaseManager = new sfDatabaseManager($this->dbParams($db_params));
    $buildSql = new Doctrine_Task_GenerateSql();
    $dropDb = new Doctrine_Task_DropDb();
    $dropDb->setArguments(array('force' => true));
    $buildSql->setArguments(array(
      'models_path' => sfConfig::get('sf_lib_dir') . '/model/doctrine',
      'sql_path' => sfConfig::get('sf_data_dir') . '/sql',
    ));
    $createDb = new Doctrine_Task_CreateDb();
    try {
      if ($dropDb->validate()) {
        $dropDb->execute();
      } else {
        throw new Doctrine_Exception($dropDb->ask());
      }
    } catch (Exception $e) {
      $installed[] = 'Could not drop DB! : ' . "\n" . $e->getMessage();
    }
    try {
      if ($createDb->validate()) {
        $createDb->execute();
      } else {
        throw new Doctrine_Exception($createDb->ask());
      }
      $installed[] = 'Successfully created database';
    } catch (Exception $e) {
      $installed[] = 'Could not create DB! : ' . "\n" . $e->getMessage();
    }
    try {
      if ($buildSql->validate()) {
        $buildSql->execute();
      }
      $installed[] = 'Successfully built SQL';
    } catch (Exception $e) {
      $installed[] = 'Could not build SQL! : ' . "\n" . $e->getMessage();
    }

    try {
      $allmodels = Doctrine_Core::loadModels(
              sfConfig::get('sf_lib_dir') . '/model/doctrine',
              Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
      $installed[] = 'Sucessefully loaded all data models';
    } catch (Exception $e) {
      $installed[] = 'Could not load models! : ' . "\n" . $e->getMessage();
    }
    try {
      Doctrine_Core::createTablesFromArray(Doctrine_Core::getLoadedModels());
      $installed[] = 'Successfully created database tables from models';
    } catch (Exception $e) {

      $installed[] = 'Could not create tables! : ' . "\n" . $e->getMessage();
    }

     try {
        Doctrine_Core::loadData(sfConfig::get('sf_data_dir') . '/fixtures', false);
        //$installed[] = 'Successfully loaded core data fixtures';
        $installed = 'Success!';
      } catch (Exception $e) {
        $installed[] = 'Could not insert SQL! : ' . "\n" . $e->getMessage();
      }
//
//    $packages = agPluginManager::getPackagesByStatus(1); //get all enabled packages
//    foreach($packages as $package)
//    {
//      try {
////        if($package == 'agStaffPackage'){
////          Doctrine_Core::loadModels(sfConfig::get('sf_lib_dir') . '/model/doctrine', Doctrine_Core::MODEL_LOADING_AGGRESSIVE);
////        }
////        else{
//          Doctrine_Core::loadModels(sfConfig::get('sf_app_dir') . '/lib/packages/' . $package . '/lib/model/doctrine', Doctrine_Core::MODEL_LOADING_AGGRESSIVE);
////        }
//        Doctrine_Core::loadData(sfConfig::get('sf_app_dir') . '/lib/packages/' . $package  . '/data/fixtures', true);
//        $installed[] = 'Successfully loaded packaged data fixtures';
//      } catch (Exception $e) {
//        $installed[] = 'Could not insert SQL! : ' . "\n" . $e->getMessage();
//      }
//    }

//    this entry is achieved by proxy of the agHost.yml fixture/example
//
//    try {
//      $ag_host = new agHost();
//      $ag_host->setHostname($this->getConfig('DB_SERVER'));
//      $ag_host->save();
//      //$installed[] = 'Successfully generated host record based on database server host';

//    } catch (Exception $e) {
//      $installed[] = 'Could not insert ag_host record ' . $e->getMessage();
//    }

    if(is_array($installed)){
      return implode('<br>', $installed);
    }
      else{
      return $installed;
    }

  }

  function EventHandler()
  {
      //echo "this is event handler";
    if (isset($_REQUEST['back'][$this->getStep()])){
      $this->DoBack();
      //echo "Doing the back step";
      }

    if ($this->getStep() == 1) {
        echo "step is 1";
         //redirect('http://www.google.lk/');
      if (!isset($_REQUEST['next'][0]) && !isset($_REQUEST['back'][2])) {
        $this->setConfig('agree', isset($_REQUEST['agree']));
        //$this->doNext();
      }

      if (isset($_REQUEST['next'][$this->getStep()]) && $this->getConfig('agree', false)) {
        $this->DoNext();
      }
    }

    $foo = $this->getStep();
    $zoo = $_REQUEST['next'][$this->getStep()];
    $poo = $_REQUEST['problem'];

    if ($this->getStep() == 2 && isset($_REQUEST['next'][$this->getStep()]) && !isset($_REQUEST['problem'])) {
      $this->dbParams($db_params);
      $this->DoNext();
    }
    if ($this->getStep() == 3) {
//on our first pass, these values won't exist (or if someone has returned with no POST
      $current = $this->getCurrent();
      $db_params = array(
        'dsn' => buildDsnString('mysql', $_POST['db_host'], $_POST['db_name']), // ilya 2010-07-21 15:16:58
//'dsn' => buildDsnString($_POST['db_type'], $_POST['db_host'], $_POST['db_name'], $_POST['db_port']),
        'hostname'=>$_POST['db_host'],
        'dbname'=>$_POST['db_name'],
        'username' => $_POST['db_user'],
        'password' => $_POST['db_pass']);
      $this->setConfig('DB_SERVER', $_POST['db_host']);
      $this->setConfig('DB_DATABASE', $_POST['db_name']);
      $this->setConfig('DB_USER', $_POST['db_user']);
      $this->setConfig('DB_PASSWORD', $_POST['db_pass']);
      $this->setConfig('ADMIN_NAME', $_POST['admin_name']);
      $this->setConfig('ADMIN_EMAIL', $_POST['admin_email']);
      $config_array = array(
        'is_installed' => array('value' => 'true'),
        'sudo' => array(
          'super_user' => $current[1]['sudo']['super_user'],
          'super_pass' => $current[1]['sudo']['super_pass']),
        'admin' => array(
          'admin_name' => $this->getConfig('ADMIN_NAME'),
          'admin_email' => $this->getConfig('ADMIN_EMAIL'),
          'auth_method' => array('value' => 'bypass'),
          'log_level' => array('value' => 'default'),
        //'db_type' => $_POST['db_type'],
//'db_host' => $_POST['db_host'],
//'db_name' => $_POST['db_port'], //ilya 2010-07-21 15:17:13
          ));
//we've set our config to post values, now let's try to save
//agSaveSetup only saves config.yml and app.yml, databases.yml is not touched
//to save our databases.yml,
//$this->dbParams($db_params);
//database parameters are good here.
      if (!($this->agSaveSetup($config_array))) {
        $this->DISABLE_NEXT = true;
        unset($_REQUEST['next']);
//if we cannot save our configuration
      } else {
        $dbcheck = $this->CheckConnection($db_params);
        if ($dbcheck == 'good') {
          $this->RETRY_SUCCESS = true;
        } else {
//if we cannot establish a db connection
          $this->RETRY_SUCCESS = false;
          $this->DISABLE_NEXT = true;
          $this->ERROR_MESSAGE = $dbcheck;
//set the installer's global error message to the return of our connection attempt
          unset($_REQUEST['next']);
        }
      }
      if (isset($_REQUEST['next'][$this->getStep()])) {
//the validation comment below can be handled by:
//$this->getCurrent();
        $this->setConfig('db_config', $db_params);
        $this->setConfig('DB_SERVER', $_POST['db_host']);
        $this->setConfig('DB_DATABASE', $_POST['db_name']);
        $this->setConfig('DB_USER', $_POST['db_user']);
        $this->setConfig('DB_PASSWORD', $_POST['db_pass']);
        $this->setConfig('ADMIN_NAME', $_POST['admin_name']);
        $this->setConfig('ADMIN_EMAIL', $_POST['admin_email']);
        $this->DoNext();
//we should validate here in case someone changes correct information
      }
    }

    if ($this->getStep() == 4) {
//present user with configuration settings, show 'install button'
      if (isset($_REQUEST['next'][$this->getStep()])) {
        $this->INSTALL_RESULT = $this->doInstall($this->getConfig('db_config'));
        $this->DoNext();
      }
    }
    if (isset($_REQUEST['finish'])) {       //isset($_REQUEST['next'][$this->getStep()]
//$this->doNext();
      $sudo = $this->getCurrent();
      $sudoer = $sudo[1]['sudo']['super_user']; //get username and password from config.yml, should be cleaner.
      $supw = $sudo[1]['sudo']['super_pass'];
//authenticate with this
      try {
        $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'all', false);
        $databaseManager = new sfDatabaseManager($configuration);
//        $connection = Doctrine_Manager::connection()->connect();
        sfContext::createInstance($configuration)->dispatch();
        agSudoAuth::authenticate($sudoer, $supw);
        redirect('admin/new');
      } catch (Exception $e) {
        $this->ERROR_MESSAGE = $e->getMessage();
      }
      return;
    }

    if (isset($_REQUEST['next'][$this->getStep()])) {
      $this->DoNext();
    }
  }

  function agSaveSetup($config)
  {
    /** remember to set the superadmin config.yml up!!!  */
    sfClearCache('frontend', 'all');
    sfClearCache('frontend', 'dev');
    sfClearCache('frontend', 'prod');
    require_once 'sfYaml.php';
    $file = 'config.yml';
// update config.yml
    try {
      file_put_contents($file, sfYaml::dump($config, 4));
    } catch (Exception $e) {
      echo "hey, something went wrong:" . $e->getMessage();
    }

    $file = 'app.yml';
    touch($file);
//if app.yml doesn't exist
    $appConfig = sfYaml::load($file);
    $appConfig['all']['sf_guard_plugin'] =
        array('check_password_callable'
          => array('agSudoAuth', 'authenticate'));
    $appConfig['all']['sf_guard_plugin_signin_form'] = 'agSudoSigninForm';

    $appConfig['all']['.array']['menu_top'] =
        array(
          'homepage' => array('label' => 'Home', 'route' => '@homepage'),
          'prepare' => array('label' => 'Prepare', 'route' => '@homepage'),
          'respond' => array('label' => 'Respond', 'route' => '@homepage'),
          'recover' => array('label' => 'Recover', 'route' => '@homepage'),
          'mitigate' => array('label' => 'Mitigate', 'route' => '@homepage'),
          'foo' => array('label' => 'Foo', 'route' => '@foo'),
          'modules' => array('label' => 'Modules', 'route' => '@homepage'),
          'admin' => array('label' => 'Administration', 'route' => '@admin'),
          'help' => array('label' => 'Help', 'route' => '@about'));

    $appConfig['all']['.array']['menu_children'] =
        array(
          'facility' => array('label' => 'Facilities', 'route' => '@facility', 'parent' => 'prepare'),
          'org' => array('label' => 'Organizations', 'route' => '@org', 'parent' => 'prepare'),
          'scenario' => array('label' => 'Scenarios', 'route' => '@scenario', 'parent' => 'prepare'),
          'activate' => array('label' => 'Activate a Scenario', 'route' => '@scenario', 'parent' => 'respond'),
          'event' => array('label' => 'Manage Events', 'route' => '@homepage', 'parent' => 'respond'),
          'event_active' => array('label' => 'Manage [Active Event]', 'route' => '@homepage', 'parent' => 'respond'));

    $appConfig['all']['.array']['menu_grandchildren'] =
        array(
          'facility_new' => array('label' => 'Add New Facility', 'route' => 'facility/new', 'parent' => 'facility'),
          'facility_list' => array('label' => 'List Facilities', 'route' => 'facility/list', 'parent' => 'facility'),
          'facility_import' => array('label' => 'Import Facilities', 'route' => '@facility', 'parent' => 'facility'),
          'facility_export' => array('label' => 'Export Facilities', 'route' => '@facility', 'parent' => 'facility'),
          'facility_types' => array('label' => 'Manage Facility Types', 'route' => '@facility', 'parent' => 'facility'),
          'org_new' => array('label' => 'Add New Organization', 'route' => 'organization/new', 'parent' => 'org'),
          'org_list' => array('label' => 'List Organizations', 'route' => 'organization/list', 'parent' => 'org'),
          'scenario_create' => array('label' => 'Build New Scenario', 'route' => 'scenario/new', 'parent' => 'scenario'),
          'scenario_list' => array('label' => 'List Scenarios', 'route' => 'scenario/list', 'parent' => 'scenario'),
          'scenario_facilitygrouptypes' => array('label' => 'Edit Facility Group Types', 'route' => 'scenario/grouptype', 'parent' => 'scenario'),
          'scenario_active' => array('label' => '[Active Scenario]', 'route' => '@scenario', 'parent' => 'scenario'),
          'event_active_staff' => array('label' => 'Staff', 'route' => '@homepage', 'parent' => 'event_active'),
          'event_active_facilities' => array('label' => 'Facilities', 'route' => '@homepage', 'parent' => 'event_active'),
          'event_active_clients' => array('label' => 'Clients', 'route' => '@homepage', 'parent' => 'event_active'),
          'event_active_reporting' => array('label' => 'Reporting', 'route' => '@homepage', 'parent' => 'event_active'));

// updates config.yml
    try {
      file_put_contents($file, sfYaml::dump($appConfig, 4));
    } catch (Exception $e) {
      echo 'hey, something went wrong: ', $e->getMessage();
      return false;
    }
    $file = 'databases.yml';
//the below line will fail if the permissions are not correct, should be wrapped in a try/catch
    $dbConfiguration = sfYaml::load($file);
    $dbConfiguration['all']['doctrine']['param']['attributes'] = array(
      'default_table_type' => 'INNODB',
      'default_table_charset' => 'utf8',
      'default_table_collate' => 'utf8_general_ci'
    );
    try {
      file_put_contents($file, sfYaml::dump($dbConfiguration, 4));
    } catch (Exception $e) {
      echo "hey, something went wrong:" . $e->getMessage();
      return false;
    }

    return true;
    //once save setup is complete, create entry in ag_host (needed for global params
  }
}



/** these two extended classes are for configuring doctrine */
class agDoctrineConfigureDatabaseTask extends sfYaml
{

  function execute($arguments = array(), $options = array())
  {
    //parent::execute($arguments, $options);
    if (null !== $options['app'])
    {
      //$file = sfConfig::get('sf_apps_dir').'/'.$options['app'].'/config/databases.yml';
        $file = 'databases.yml';
    }
    else
    {
      $file = 'databases.yml';
    }
    //echo file_exists($file);
    $config = file_exists($file) ? sfYaml::load($file) : array();

    $config[$options['env']][$options['name']] = array(
      'class' => $options['class'],
      'param' => array_merge(isset($config[$options['env']][$options['name']]['param']) ? $config[$options['env']][$options['name']]['param'] : array(), array('dsn' => $arguments['dsn'], 'hostname'=>$arguments['hostname'], 'dbname'=>$arguments['dbname'], 'username' => $arguments['username'], 'password' => $arguments['password'])),
    );

    file_put_contents($file, sfYaml::dump($config, 4));
  }

}

/*class agDoctrineBuildSqlTask extends sfDoctrineBuildSqlTask
{

  function execute($arguments = array(), $options = array())
  {
    try {
      parent::execute($arguments, $options);
    } catch (Exception $e) {
      throw new Doctrine_Exception($e->getMessage());
    }
  }

}*/