Exemple #1
0
 public function save(array $person = array(), $person_id = null)
 {
     $result = array('success' => false, 'error' => '');
     $valid = true;
     $person = $this->get_trimmed_allowed($person, array('space_id', 'email', 'x', 'y', 'active'));
     if (isset($person['space_id']) && !valid::digit($person['space_id'])) {
         $valid = false;
     }
     if (isset($person['email']) && !valid::email($person['email'])) {
         $valid = false;
     }
     if (isset($person['x']) && !valid::numeric($person['x'])) {
         $valid = false;
     }
     if (isset($person['y']) && !valid::numeric($person['y'])) {
         $valid = false;
     }
     if (isset($person['active']) && !valid::digit($person['active'])) {
         $valid = false;
     }
     if ($valid) {
         if ($person_id) {
             //UPDATE
             $this->db->from('people')->set($person)->where(array('id' => (int) $person_id))->update();
             $result['success'] = true;
         } else {
             // INSERT
             $new_person = $this->db->from('people')->set($person)->insert();
             $result['success'] = true;
             $person_id = $new_person->insert_id();
         }
         $person = $this->db->select('people.*')->from('people')->where(array('id' => $person_id))->get();
         $person = $this->result_as_array($person);
         $result['person'] = $person;
     } else {
         $result['error'] = "The supplied data was invalid";
     }
     return $result;
 }
 /**
  * Tests the text::random() function.
  * @dataProvider random_provider
  * @group core.helpers.text.random
  * @test
  */
 public function random($type, $length = 8)
 {
     //$this->markTestIncomplete('Test for PHP 5.3 bug needs to be counted, Kohana is still supporting 5.2');
     $result = text::random($type, $length);
     if ((string) $type) {
         // Checking length
         $this->assertEquals(mb_strlen($result), $length);
         $pool = '';
         switch ($type) {
             case 'alnum':
                 $this->assertTrue(valid::alpha_numeric($result));
                 break;
             case 'alpha':
                 $this->assertTrue(valid::alpha($result));
                 break;
             case 'numeric':
                 $this->assertTrue(valid::numeric($result));
                 break;
             case 'nozero':
                 $this->assertTrue(is_numeric($result));
                 break;
             case 'hexdec':
                 $pool = '0123456789abcdef';
                 break;
             case 'distinct':
                 $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
                 break;
             default:
                 $pool = (string) $type;
         }
         if ($pool) {
             // PHP versions before 5.3 have a bug with preg_quote and it doesn't escape '-'
             $pool = version_compare(PHP_VERSION, '5.3', '>=') ? preg_quote((string) $pool, '/') : utf8::str_ireplace('-', '\\-', preg_quote((string) $pool, '/'));
             if (preg_match('/[' . $pool . ']*/u', $result, $match)) {
                 $this->assertEquals($match[0], $result);
             } else {
                 $this->assertTrue(FALSE);
             }
         }
     } else {
         // Checking length
         $this->assertEquals($result, '');
     }
 }
Exemple #3
0
 /**
  * Validate Custom Form Fields
  * @param Validation $post Validation object from form post
  * XXX This whole function is being done backwards
  * Need to pull the list of custom form fields first
  * Then look through them to see if they're set, not the other way around.
  */
 public static function validate_custom_form_fields(&$post)
 {
     $custom_fields = array();
     if (!isset($post->custom_field)) {
         return;
     }
     /* XXX Checkboxes hackery
     			 Checkboxes are submitted in the post as custom_field[field_id-boxnum]
     			 This foreach loop consolidates them into one variable separated by commas.
     			 If no checkboxes are selected then the custom_field[] for that variable is not sent
     			 To get around that the view sets a hidden custom_field[field_id-BLANKHACK] field that
     			 ensures the checkbox custom_field is there to be tested.
     		*/
     foreach ($post->custom_field as $field_id => $field_response) {
         $split = explode("-", $field_id);
         if (isset($split[1])) {
             // The view sets a hidden field for blankhack
             if ($split[1] == 'BLANKHACK') {
                 if (!isset($custom_fields[$split[0]])) {
                     // then no checkboxes were checked
                     $custom_fields[$split[0]] = '';
                 }
                 // E.Kala - Removed the else {} block; either way continue is still invoked
                 continue;
             }
             if (isset($custom_fields[$split[0]])) {
                 $custom_fields[$split[0]] .= ",{$field_response}";
             } else {
                 $custom_fields[$split[0]] = $field_response;
             }
         } else {
             $custom_fields[$split[0]] = $field_response;
         }
     }
     $post->custom_field = $custom_fields;
     // Kohana::log('debug', Kohana::debug($custom_fields));
     foreach ($post->custom_field as $field_id => $field_response) {
         $field_param = ORM::factory('form_field', $field_id);
         $custom_name = $field_param->field_name;
         // Validate that this custom field already exists
         if (!$field_param->loaded) {
             // Populate the error field
             //$errors[$field_id] = "The $custom_name field does not exist";
             $post->add_error('custom_field', 'not_exist', array($field_id));
             return;
         }
         $max_auth = self::get_user_max_auth();
         $required_role = ORM::factory('role', $field_param->field_ispublic_submit);
         if (($required_role->loaded ? $required_role->access_level : 0) > $max_auth) {
             // Populate the error field
             $post->add_error('custom_field', 'permission', array($custom_name));
             return;
         }
         // Validate that the field is required
         if ($field_param->field_required == 1 and $field_response == "") {
             $post->add_error('custom_field', 'required', array($custom_name));
             return;
         }
         // Grab the custom field options for this field
         $field_options = self::get_custom_field_options($field_id);
         // Validate Custom fields for text boxes
         if ($field_param->field_type == 1 and isset($field_options) and $field_response != '') {
             if (isset($field_options['field_datatype'])) {
                 if ($field_options['field_datatype'] == 'email' and !valid::email($field_response)) {
                     $post->add_error('custom_field', 'email', array($custom_name));
                 }
                 if ($field_options['field_datatype'] == 'phonenumber' and !valid::phone($field_response)) {
                     $post->add_error('custom_field', 'phone', array($custom_name));
                 }
                 if ($field_options['field_datatype'] == 'numeric' and !valid::numeric($field_response)) {
                     $post->add_error('custom_field', 'numeric', array($custom_name));
                 }
             }
         }
         // Validate for date
         if ($field_param->field_type == 3 and $field_response != "") {
             $field_default = $field_param->field_default;
             if (!valid::date_mmddyyyy($field_response)) {
                 $post->add_error('custom_field', 'date_mmddyyyy', array($custom_name));
             }
         }
         // Validate multi-value boxes only have acceptable values
         if ($field_param->field_type >= 5 and $field_param->field_type <= 7) {
             $defaults = explode('::', $field_param->field_default);
             $options = array();
             if (preg_match("/[0-9]+-[0-9]+/", $defaults[0]) and count($defaults) == 1) {
                 $dashsplit = explode('-', $defaults[0]);
                 $start = $dashsplit[0];
                 $end = $dashsplit[1];
                 for ($i = $start; $i <= $end; $i++) {
                     array_push($options, $i);
                 }
             } else {
                 $options = array_map('trim', explode(',', $defaults[0]));
             }
             $responses = explode(',', $field_response);
             foreach ($responses as $response) {
                 if (!in_array($response, $options) and $response != '') {
                     $post->add_error('custom_field', 'values', array($custom_name));
                     //$errors[$field_id] = "The $custom_name field does not include $response as an option";
                 }
             }
         }
         // Validate that a required checkbox is checked
         if ($field_param->field_type == 6 and $field_response == 'BLANKHACK' and $field_param->field_required == 1) {
             $post->add_error('custom_field', 'required', array($custom_name));
         }
     }
     return;
 }
Exemple #4
0
 public function valid_numeric_test()
 {
     $this->assert_true_strict(valid::numeric(-12.99))->assert_false_strict(valid::numeric('123_4'));
 }
Exemple #5
0
 /**
  * Tests the valid::numeric() function.
  * @dataProvider numeric_provider
  * @group core.helpers.valid.numeric
  * @test
  */
 public function numeric($input_numeric, $expected_result)
 {
     $result = valid::numeric($input_numeric);
     $this->assertEquals($expected_result, $result);
 }
Exemple #6
0
 /**
  * Tests the text::random() function.
  * @dataProvider random_provider
  * @group core.helpers.text.random
  * @test
  */
 public function random($type, $length = 8)
 {
     $this->markTestIncomplete('Test for PHP 5.3 bug needs to be counted, Kohana is still supporting 5.2');
     $result = text::random($type, $length);
     if ((string) $type) {
         // Checking length
         $this->assertEquals(utf8::strlen($result), $length);
         $pool = '';
         switch ($type) {
             case 'alnum':
                 $this->assertTrue(valid::alpha_numeric($result));
                 break;
             case 'alpha':
                 $this->assertTrue(valid::alpha($result));
                 break;
             case 'numeric':
                 $this->assertTrue(valid::numeric($result));
                 break;
             case 'nozero':
                 $this->assertTrue(is_numeric($result));
                 break;
             case 'hexdec':
                 $pool = '0123456789abcdef';
                 break;
             case 'distinct':
                 $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
                 break;
             default:
                 $pool = (string) $type;
         }
         if ($pool) {
             if (preg_match('/[' . preg_quote((string) $pool, '/') . ']*/u', $result, $match)) {
                 $this->assertEquals($match[0], $result);
             } else {
                 $this->assertTrue(FALSE);
             }
         }
     } else {
         // Checking length
         $this->assertEquals($result, '');
     }
 }