diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 846202885125b7e1f9611f815f6ce2f236187d04..6df00e2b4c062b4a5b65011a7e34ed61f76690ec 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -350,7 +350,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 97c27e922a28690605cfc9ffb4c1bfc82a2158ff..9c0cc0b62c6e5a41967ca6e7e7fae142225631e3 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -509,23 +509,58 @@ class TextareaField extends FormField { class PhoneField extends FormField { static $widget = 'PhoneNumberWidget'; + function getConfigurationOptions() { + return array( + 'ext' => new BooleanField(array( + 'label'=>'Extension', 'default'=>true, + 'configuration'=>array( + 'desc'=>'Add a separate field for the extension', + ), + )), + 'digits' => new TextboxField(array( + 'label'=>'Minimum length', 'default'=>7, + 'hint'=>'Fewest digits allowed in a valid phone number', + 'configuration'=>array('validator'=>'number', 'size'=>5), + )), + 'format' => new ChoiceField(array( + 'label'=>'Display format', 'default'=>'us', + 'choices'=>array(''=>'-- Unformatted --', + 'us'=>'United States'), + )), + ); + } + function validateEntry($value) { parent::validateEntry($value); + $config = $this->getConfiguration(); # Run validator against $this->value for email type list($phone, $ext) = explode("X", $value, 2); - if ($phone && !Validator::is_phone($phone)) + if ($phone && ( + !is_numeric($phone) || + strlen($phone) < $config['digits'])) $this->_errors[] = "Enter a valid phone number"; - if ($ext) { + if ($ext && $config['ext']) { if (!is_numeric($ext)) - $this->_errors[] = "Enter a valide phone extension"; + $this->_errors[] = "Enter a valid phone extension"; elseif (!$phone) $this->_errors[] = "Enter a phone number for the extension"; } } + function parse($value) { + // NOTE: Value may have a legitimate 'X' to separate the number and + // extension parts. Don't remove the 'X' + return preg_replace('/[^\dX]/', '', $value); + } + function toString($value) { + $config = $this->getConfiguration(); list($phone, $ext) = explode("X", $value, 2); - $phone=Format::phone($phone); + switch ($config['format']) { + case 'us': + $phone = Format::phone($phone); + break; + } if ($ext) $phone.=" x$ext"; return $phone; @@ -574,7 +609,7 @@ class ChoiceField extends FormField { if (is_numeric($value)) return $value; foreach ($this->getChoices() as $k=>$v) - if (strcasecmp($value, $v) === 0) + if (strcasecmp($value, $k) === 0) return $k; } @@ -833,12 +868,17 @@ class TextareaWidget extends Widget { class PhoneNumberWidget extends Widget { function render() { + $config = $this->field->getConfiguration(); list($phone, $ext) = explode("X", $this->value); ?> <input type="text" name="<?php echo $this->name; ?>" value="<?php - echo $phone; ?>"/> Ext: <input type="text" name="<?php + echo $phone; ?>"/><?php + // Allow display of extension field even if disabled if the phone + // number being edited has an extension + if ($ext || $config['ext']) { ?> Ext: + <input type="text" name="<?php echo $this->name; ?>-ext" value="<?php echo $ext; ?>" size="5"/> - <?php + <?php } } function getValue() { @@ -847,6 +887,7 @@ class PhoneNumberWidget extends Widget { if ($base === null) return $base; $ext = $data["{$this->name}-ext"]; + // NOTE: 'X' is significant. Don't change it if ($ext) $ext = 'X'.$ext; return $base . $ext; } diff --git a/include/class.ticket.php b/include/class.ticket.php index 8afb80f38fc10098fc729dff987152221d1e7508..fc6499529e616f9f1eea0557a916d64e93e92299 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 3366bdeb4ef42b2196578234c328fdcbf5808428..b1aae2e7c993cb56e217444555ff0da26ee5c7dd 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 ff8cec8ffe7aaa67396f5db7de3247183e919196..c11b606685db5059b39b6e779debb7c5f0742a65 100644 --- a/include/staff/ticket-view.inc.php +++ b/include/staff/ticket-view.inc.php @@ -183,6 +183,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