From 7761b16549363febaa0b4e447d379af219d64661 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 4 Nov 2013 21:34:59 +0000
Subject: [PATCH] Fix various phone number issues

Fixes #77
---
 include/class.dynamic_forms.php   |  2 +-
 include/class.forms.php           |  4 ++++
 include/class.ticket.php          |  9 ++++-----
 include/class.user.php            | 19 +++++++++++++++----
 include/staff/ticket-view.inc.php |  4 ++++
 5 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 7771802bc..618602df3 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -336,7 +336,7 @@ class DynamicFormEntry extends VerySimpleModel {
     function getAnswer($name) {
         foreach ($this->getAnswers() as $ans)
             if ($ans->getField()->get('name') == $name)
-                return $ans->getValue();
+                return $ans;
         return null;
     }
     function setAnswer($name, $value, $id=false) {
diff --git a/include/class.forms.php b/include/class.forms.php
index 356c504da..c815c978f 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -514,6 +514,10 @@ class PhoneField extends FormField {
         }
     }
 
+    function to_database($value) {
+        return preg_replace('/[()+. -]/', '', $value);
+    }
+
     function toString($value) {
         list($phone, $ext) = explode("X", $value, 2);
         $phone=Format::phone($phone);
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 8afb80f38..fc6499529 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -303,17 +303,17 @@ class Ticket {
     }
 
     function getPhone() {
-        list($phone, $ext) = explode(" ", $this->_answers['phone'], 2);
+        list($phone, $ext) = $this->getPhoneNumber();
         return $phone;
     }
 
     function getPhoneExt() {
-        list($phone, $ext) = explode(" ", $this->_answers['phone'], 2);
+        list($phone, $ext) = $this->getPhoneNumber();
         return $ext;
     }
 
     function getPhoneNumber() {
-        return $this->_answers['phone'];
+        return (string)$this->getOwner()->getPhoneNumber();
     }
 
     function getSource() {
@@ -1891,8 +1891,7 @@ class Ticket {
         // fields into local scope for filtering and banning purposes
         $user_form = UserForm::getInstance();
         $user_info = $user_form->getClean();
-        if ($user_form->isValid())
-            $vars += $user_info;
+        $vars += $user_info;
 
         //Check for 403
         if ($vars['email']  && Validator::is_email($vars['email'])) {
diff --git a/include/class.user.php b/include/class.user.php
index 3366bdeb4..b1aae2e7c 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -79,11 +79,14 @@ class User extends UserModel {
         // Try and lookup by email address
         $user = User::lookup(array('emails__address'=>$data['email']));
         if (!$user) {
-            $user = User::create(array('name'=>$data['name'],
+            $user = User::create(array(
+                'name'=>$data['name'],
+                'created'=>new SqlFunction('NOW'),
+                'updated'=>new SqlFunction('NOW'),
                 'default_email'=>
                     UserEmail::create(array('address'=>$data['email']))
             ));
-            $user->save();
+            $user->save(true);
             $user->emails->add($user->default_email);
 
             // Attach initial custom fields
@@ -106,6 +109,12 @@ class User extends UserModel {
         return $this->name;
     }
 
+    function getPhoneNumber() {
+        foreach ($this->getDynamicData() as $e)
+            if ($a = $e->getAnswer('phone'))
+                return $a;
+    }
+
     function getName() {
         return new PersonsName($this->name);
     }
@@ -120,8 +129,8 @@ class User extends UserModel {
 
         $tag = strtolower($tag);
         foreach ($this->getDynamicData() as $e)
-            if ($e->getAnswer($tag))
-                return $e;
+            if ($a = $e->getAnswer($tag))
+                return $a;
     }
 
     function getDynamicData() {
@@ -150,6 +159,8 @@ class User extends UserModel {
                 $this->name = $parts[1].' '.$parts[0].' '.$parts[2];
                 break;
         }
+        if (count($this->dirty))
+            $this->set('updated', new SqlFunction('NOW'));
         return parent::save($refetch);
     }
 }
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 78714bbd3..c4ff81701 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -182,6 +182,10 @@ if($ticket->isOverdue())
                     <?php echo $ticket->getEmail(); ?>
                     </td>
                 </tr>
+                <tr>
+                    <th>Phone:</th>
+                    <td><?php echo $ticket->getPhoneNumber(); ?></td>
+                </tr>
                 <tr>
                     <th>Source:</th>
                     <td><?php
-- 
GitLab