From e055cc21404673e4d58ae60b224a49456cbe0d61 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 27 Sep 2013 03:53:09 +0000
Subject: [PATCH] Fixup the installer

Fixup several minor bugs concerning initial experience
---
 bootstrap.php                             |  6 ++--
 include/ajax.forms.php                    |  7 ++---
 include/class.dynamic_forms.php           |  2 +-
 include/class.forms.php                   |  8 +++--
 include/class.ticket.php                  |  4 +--
 include/i18n/en_US/form.yaml              |  1 +
 include/i18n/en_US/templates/premade.yaml |  2 +-
 include/staff/dynamic-form.inc.php        | 10 +++---
 include/upgrader/streams/core.sig         |  2 +-
 setup/inc/class.installer.php             | 37 +++++++++++------------
 setup/inc/streams/core/install-mysql.sql  |  6 ++--
 setup/setup.inc.php                       |  3 --
 12 files changed, 42 insertions(+), 46 deletions(-)

diff --git a/bootstrap.php b/bootstrap.php
index 55b747cb6..ac25ecf31 100644
--- a/bootstrap.php
+++ b/bootstrap.php
@@ -176,8 +176,8 @@ class Bootstrap {
         require(INCLUDE_DIR.'class.signal.php');
         require(INCLUDE_DIR.'class.nav.php');
         require(INCLUDE_DIR.'class.page.php');
-        require(INCLUDE_DIR.'class.format.php'); //format helpers
-        require(INCLUDE_DIR.'class.validator.php'); //Class to help with basic form input validation...please help improve it.
+        require_once(INCLUDE_DIR.'class.format.php'); //format helpers
+        require_once(INCLUDE_DIR.'class.validator.php'); //Class to help with basic form input validation...please help improve it.
         require(INCLUDE_DIR.'class.mailer.php');
         if (extension_loaded('mysqli'))
             require_once INCLUDE_DIR.'mysqli.php';
@@ -206,7 +206,7 @@ define('I18N_DIR', INCLUDE_DIR.'i18n/');
 /*############## Do NOT monkey with anything else beyond this point UNLESS you really know what you are doing ##############*/
 
 #Current version && schema signature (Changes from version to version)
-define('THIS_VERSION','1.8.0-devel'); //Shown on admin panel
+define('THIS_VERSION','1.8-git'); //Shown on admin panel
 //Path separator
 if(!defined('PATH_SEPARATOR')){
     if(strpos($_ENV['OS'],'Win')!==false || !strcasecmp(substr(PHP_OS, 0, 3),'WIN'))
diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index 17cae9189..ee95f722a 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -38,11 +38,8 @@ class DynamicFormsAjaxAPI extends AjaxController {
 
     function _getUserForms() {
         $static = new Form(array(
-            'first' => new TextboxField(array(
-                'label'=>'First Name', 'configuration'=>array('size'=>30))
-            ),
-            'last' => new TextboxField(array(
-                'label'=>'Last Name', 'configuration'=>array('size'=>30))
+            'name' => new TextboxField(array(
+                'label'=>'Full Name', 'configuration'=>array('size'=>40))
             ),
             'email' => new TextboxField(array(
                 'label'=>'Default Email', 'configuration'=>array(
diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index f38f09bf6..7f034a05e 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -242,7 +242,7 @@ class DynamicFormField extends VerySimpleModel {
     function isDeletable() {
         return $this->get('edit_mask') & 1;
     }
-    function isNameEditable() {
+    function isNameForced() {
         return $this->get('edit_mask') & 2;
     }
     function isPrivacyForced() {
diff --git a/include/class.forms.php b/include/class.forms.php
index b9a6a2dbe..da4ed00a9 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -267,14 +267,18 @@ class FormField {
      */
     function getImpl($parent=null) {
         // Allow registration with ::addFieldTypes and delayed calling
-        $this->parent = $parent;
         $type = static::getFieldType($this->get('type'));
         $clazz = $type[1];
-        return new $clazz($this->ht);
+        $inst = new $clazz($this->ht);
+        $inst->parent = $parent;
+        return $inst;
     }
 
     function __call($what, $args) {
         // XXX: Throw exception if $this->parent is not set
+        // BEWARE: DynamicFormField has a __call() which will create a new
+        //      FormField instance and invoke __call() on it or bounce
+        //      immediately back
         return call_user_func_array(
             array($this->parent, $what), $args);
     }
diff --git a/include/class.ticket.php b/include/class.ticket.php
index ef82b7104..9bf716e39 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -62,14 +62,12 @@ class Ticket {
         if(!$id && !($id=$this->getId()))
             return false;
 
-        $sql='SELECT  ticket.*, lock_id, dept_name, priority_desc '
+        $sql='SELECT  ticket.*, lock_id, dept_name '
             .' ,IF(sla.id IS NULL, NULL, DATE_ADD(ticket.created, INTERVAL sla.grace_period HOUR)) as sla_duedate '
             .' ,count(attach.attach_id) as attachments '
             .' FROM '.TICKET_TABLE.' ticket '
             .' LEFT JOIN '.DEPT_TABLE.' dept ON (ticket.dept_id=dept.dept_id) '
             .' LEFT JOIN '.SLA_TABLE.' sla ON (ticket.sla_id=sla.id AND sla.isactive=1) '
-            .' LEFT JOIN '.TICKET_PRIORITY_TABLE.' pri ON ('
-                .'ticket.priority_id=pri.priority_id) '
             .' LEFT JOIN '.TICKET_LOCK_TABLE.' tlock ON ('
                 .'ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW()) '
             .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON ('
diff --git a/include/i18n/en_US/form.yaml b/include/i18n/en_US/form.yaml
index 1de219b60..745948812 100644
--- a/include/i18n/en_US/form.yaml
+++ b/include/i18n/en_US/form.yaml
@@ -79,5 +79,6 @@
       name: priority # notrans
       label: Priority Level
       required: false
+      private: true
       edit_mask: 3
       sort: 3
diff --git a/include/i18n/en_US/templates/premade.yaml b/include/i18n/en_US/templates/premade.yaml
index 8483de8b9..fa73ee8f9 100644
--- a/include/i18n/en_US/templates/premade.yaml
+++ b/include/i18n/en_US/templates/premade.yaml
@@ -17,7 +17,7 @@
 
 - title: Sample (with variables)
   response: |
-    %{ticket.name},
+    Hi %{ticket.name.first},
 
     Your ticket #%{ticket.number} created on %{ticket.create_date} is in
     %{ticket.dept.name} department.
diff --git a/include/staff/dynamic-form.inc.php b/include/staff/dynamic-form.inc.php
index 34ba48ffd..bdb38041c 100644
--- a/include/staff/dynamic-form.inc.php
+++ b/include/staff/dynamic-form.inc.php
@@ -46,6 +46,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
     </tbody>
     </table>
     <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
+    <?php if ($form && $form->get('type') == 'T') { ?>
     <thead>
         <tr>
             <th colspan="7">
@@ -89,6 +90,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
 
         <?php } ?>
     </tbody>
+    <?php } # form->type == 'T' ?>
     <thead>
         <tr>
             <th colspan="7">
@@ -107,10 +109,10 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
     <tbody class="sortable-rows" data-sort="sort-">
     <?php if ($form) foreach ($form->getFields() as $f) {
         $id = $f->get('id');
-        $deletable = ($f->get('editable') & 1) ? 'disabled="disabled"' : '';
-        $force_name = ($f->get('editable') & 2) ? 'disabled="disabled"' : '';
-        $force_privacy = ($f->get('editable') & 4) ? 'disabled="disabled"' : '';
-        $force_required = ($f->get('editable') & 8) ? 'disabled="disabled"' : '';
+        $deletable = $f->isDeletable() ? 'disabled="disabled"' : '';
+        $force_name = $f->isNameForced() ? 'disabled="disabled"' : '';
+        $force_privacy = $f->isPrivacyForced() ? 'disabled="disabled"' : '';
+        $force_required = $f->isRequirementForced() ? 'disabled="disabled"' : '';
         $errors = $f->errors(); ?>
         <tr>
             <td><input type="checkbox" name="delete-<?php echo $id; ?>"
diff --git a/include/upgrader/streams/core.sig b/include/upgrader/streams/core.sig
index a54c1899f..55533fd92 100644
--- a/include/upgrader/streams/core.sig
+++ b/include/upgrader/streams/core.sig
@@ -1 +1 @@
-4e69fdfbda59040e51c2d93c874fcf22
+6871646b8ea2848fc0039a601e1e7ce4
diff --git a/setup/inc/class.installer.php b/setup/inc/class.installer.php
index 6ec2fa717..ad2ee14a3 100644
--- a/setup/inc/class.installer.php
+++ b/setup/inc/class.installer.php
@@ -15,7 +15,6 @@
 **********************************************************************/
 require_once INCLUDE_DIR.'class.migrater.php';
 require_once INCLUDE_DIR.'class.setup.php';
-require_once INCLUDE_DIR.'class.i18n.php';
 
 class Installer extends SetupWizard {
 
@@ -111,6 +110,7 @@ class Installer extends SetupWizard {
         define('ADMIN_EMAIL',$vars['admin_email']); //Needed to report SQL errors during install.
         define('PREFIX',$vars['prefix']); //Table prefix
         Bootstrap::defineTables(PREFIX);
+        Bootstrap::loadCode();
 
         $debug = true; // Change it to false to squelch SQL errors.
 
@@ -143,6 +143,8 @@ class Installer extends SetupWizard {
 
         if(!$this->errors) {
             // TODO: Use language selected from install worksheet
+            require_once INCLUDE_DIR.'class.i18n.php';
+
             $i18n = new Internationalization('en_US');
             $i18n->loadDefaultData();
 
@@ -232,25 +234,22 @@ class Installer extends SetupWizard {
             .", autoresp_email_id=$support_email_id";
         db_query($sql, false);
 
+        global $cfg;
+        $cfg = new OsticketConfig();
+
         //Create a ticket to make the system warm and happy.
-        $sql='INSERT INTO '.PREFIX.'ticket SET created=NOW(), status="open", source="Web" '
-            ." ,priority_id=0, dept_id=$dept_id_1, topic_id=0 "
-            .' ,ticketID='.db_input(Misc::randNumber(6))
-            .' ,email="support@osticket.com" '
-            .' ,name="osTicket Support" '
-            .' ,subject="osTicket Installed!"';
-        if(db_query($sql, false) && ($tid=db_insert_id())) {
-            if(!($msg=file_get_contents(INC_DIR.'msg/installed.txt')))
-                $msg='Congratulations and Thank you for choosing osTicket!';
-
-            $sql='INSERT INTO '.PREFIX.'ticket_thread SET created=NOW()'
-                .', source="Web" '
-                .', thread_type="M" '
-                .', ticket_id='.db_input($tid)
-                .', title='.db_input('osTicket Installed')
-                .', body='.db_input($msg);
-            db_query($sql, false);
-        }
+        if(!($msg=file_get_contents(INC_DIR.'msg/installed.txt')))
+            $msg='Congratulations and Thank you for choosing osTicket!';
+        $errors = array();
+        $tid = Ticket::create(array(
+            'email' =>      'support@osticket.com',
+            'name' =>       'osTicket Support',
+            'subject' =>    'osTicket Installed!',
+            'message' =>    $msg,
+            'source' =>     'email',
+            'deptId' =>     $dept_id_1),
+            $errors,
+            'api', false, false);
         //TODO: create another personalized ticket and assign to admin??
 
         //Log a message.
diff --git a/setup/inc/streams/core/install-mysql.sql b/setup/inc/streams/core/install-mysql.sql
index 78d3a68c5..aa71d8cfe 100644
--- a/setup/inc/streams/core/install-mysql.sql
+++ b/setup/inc/streams/core/install-mysql.sql
@@ -191,7 +191,7 @@ CREATE TABLE `%TABLE_PREFIX%form_field` (
     `label` varchar(255) NOT NULL,
     `required` tinyint(1) NOT NULL DEFAULT 0,
     `private` tinyint(1) NOT NULL DEFAULT 0,
-    `edit_mask` tinyint(1) NOT NULL DEFAULT 1,
+    `edit_mask` tinyint(1) NOT NULL DEFAULT 0,
     `name` varchar(64) NOT NULL,
     `configuration` text,
     `sort` int(11) unsigned NOT NULL,
@@ -211,7 +211,7 @@ CREATE TABLE `%TABLE_PREFIX%form_entry` (
     `created` datetime NOT NULL,
     `updated` datetime NOT NULL,
     PRIMARY KEY (`id`),
-    KEY `ticket_dyn_form_lookup` (`ticket_id`)
+    KEY `entry_lookup` (`object_id`, `object_type`)
 ) DEFAULT CHARSET=utf8;
 
 DROP TABLE IF EXISTS `%TABLE_PREFIX%form_entry_values`;
@@ -587,7 +587,6 @@ CREATE TABLE `%TABLE_PREFIX%ticket` (
   `user_email_id` int(11) unsigned NOT NULL default '0',
   `dept_id` int(10) unsigned NOT NULL default '0',
   `sla_id` int(10) unsigned NOT NULL default '0',
-  `priority_id` int(10) unsigned NOT NULL default '0',
   `topic_id` int(10) unsigned NOT NULL default '0',
   `staff_id` int(10) unsigned NOT NULL default '0',
   `team_id` int(10) unsigned NOT NULL default '0',
@@ -608,7 +607,6 @@ CREATE TABLE `%TABLE_PREFIX%ticket` (
   KEY `staff_id` (`staff_id`),
   KEY `team_id` (`staff_id`),
   KEY `status` (`status`),
-  KEY `priority_id` (`priority_id`),
   KEY `created` (`created`),
   KEY `closed` (`closed`),
   KEY `duedate` (`duedate`),
diff --git a/setup/setup.inc.php b/setup/setup.inc.php
index c68bd7a62..6eb4a495f 100644
--- a/setup/setup.inc.php
+++ b/setup/setup.inc.php
@@ -14,9 +14,6 @@
     vim: expandtab sw=4 ts=4 sts=4:
 **********************************************************************/
 
-#This  version - changed on every release
-define('THIS_VERSION', '1.8-git');
-
 #inits - error reporting.
 $error_reporting = E_ALL & ~E_NOTICE;
 if (defined('E_STRICT')) # 5.4.0
-- 
GitLab