Esempio n. 1
0
 /**
  * Change the value or perform actions after validation but before saving to the DB
  *
  * @param mixed $value
  * @param int $id
  * @param string $name
  * @param array $options
  * @param array $fields
  * @param array $pod
  * @param object $params
  *
  * @since 2.0
  */
 public function pre_save($value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null)
 {
     $options = (array) $options;
     if ('international' == pods_var(self::$type . '_format', $options)) {
         // no validation/changes
     } else {
         // Clean input
         $number = preg_replace('/([^0-9ext])/', '', $value);
         $number = str_replace(array('-', '.', 'ext', 'x', 't', 'e', '(', ')'), array('', '', '|', '|', '', '', '', ''), $number);
         // Get extension
         $extension = explode('|', $number);
         if (1 < count($extension)) {
             $number = preg_replace('/([^0-9])/', '', $extension[0]);
             $extension = preg_replace('/([^0-9])/', '', $extension[1]);
         } else {
             $extension = '';
         }
         // Build number array
         $numbers = str_split($number, 3);
         if (isset($numbers[3])) {
             $numbers[2] .= $numbers[3];
             $numbers = array($numbers[0], $numbers[1], $numbers[2]);
         } elseif (isset($numbers[1])) {
             $numbers = array($numbers[0], $numbers[1]);
         }
         // Format number
         if ('(999) 999-9999 x999' == pods_var(self::$type . '_format', $options)) {
             if (2 == count($numbers)) {
                 $value = implode('-', $numbers);
             } else {
                 $value = '(' . $numbers[0] . ') ' . $numbers[1] . '-' . $numbers[2];
             }
         } elseif ('999.999.9999 x999' == pods_var(self::$type . '_format', $options)) {
             $value = implode('.', $numbers);
         } else {
             //if ( '999-999-9999 x999' == pods_var( self::$type . '_format', $options ) )
             $value = implode('-', $numbers);
         }
         // Add extension
         if (1 == pods_var(self::$type . '_enable_phone_extension', $options) && 0 < strlen($extension)) {
             $value .= ' x' . $extension;
         }
     }
     $length = (int) pods_var(self::$type . '_max_length', $options, 25);
     if (0 < $length && $length < pods_mb_strlen($value)) {
         $value = pods_mb_substr($value, 0, $length);
     }
     return $value;
 }
 /**
  * Change the value or perform actions after validation but before saving to the DB
  *
  * @param mixed $value
  * @param int $id
  * @param string $name
  * @param array $options
  * @param array $fields
  * @param array $pod
  * @param object $params
  *
  * @return mixed|string
  * @since 2.0
  */
 public function pre_save($value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null)
 {
     $length = (int) pods_var(self::$type . '_max_length', $options, 255);
     if (0 < $length && $length < pods_mb_strlen($value)) {
         $value = pods_mb_substr($value, 0, $length);
     }
     return $value;
 }
 /**
  * Change the value or perform actions after validation but before saving to the DB
  *
  * @param mixed $value
  * @param int $id
  * @param string $name
  * @param array $options
  * @param array $fields
  * @param array $pod
  * @param object $params
  *
  * @since 2.0
  */
 public function pre_save($value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null)
 {
     $options = (array) $options;
     if (is_array($value)) {
         if (isset($value['scheme'])) {
             $value = $this->build_url($value);
         } else {
             $value = implode('', $value);
         }
     }
     if (false === strpos($value, '://') && 0 !== strpos($value, '//')) {
         $value = 'http://' . $value;
     }
     $url = @parse_url($value);
     if (empty($url) || count($url) < 2) {
         $value = '';
     } else {
         $defaults = array('scheme' => 'http', 'host' => '', 'path' => '/', 'query' => '', 'fragment' => '');
         $url = array_merge($defaults, $url);
         if ('normal' == pods_var(self::$type . '_format', $options)) {
             $value = $this->build_url($url);
         } elseif ('no-www' == pods_var(self::$type . '_format', $options)) {
             if (0 === strpos($url['host'], 'www.')) {
                 $url['host'] = substr($url['host'], 4);
             }
             $value = $this->build_url($url);
         } elseif ('force-www' == pods_var(self::$type . '_format', $options)) {
             if (false !== strpos($url['host'], '.') && false === strpos($url['host'], '.', 1)) {
                 $url['host'] = 'www.' . $url['host'];
             }
             $value = $this->build_url($url);
         } elseif ('no-http' == pods_var(self::$type . '_format', $options)) {
             $value = $this->build_url($url);
             $value = str_replace(trim($url['scheme'] . '://', ':'), '', $value);
             if ('/' == $url['path']) {
                 $value = trim($value, '/');
             }
         } elseif ('no-http-no-www' == pods_var(self::$type . '_format', $options)) {
             if (0 === strpos($url['host'], 'www.')) {
                 $url['host'] = substr($url['host'], 4);
             }
             $value = $this->build_url($url);
             $value = str_replace(trim($url['scheme'] . '://', ':'), '', $value);
             if ('/' == $url['path']) {
                 $value = trim($value, '/');
             }
         } elseif ('no-http-force-www' == pods_var(self::$type . '_format', $options)) {
             if (false !== strpos($url['host'], '.') && false === strpos($url['host'], '.', 1)) {
                 $url['host'] = 'www.' . $url['host'];
             }
             $value = $this->build_url($url);
             $value = str_replace(trim($url['scheme'] . '://', ':'), '', $value);
             if ('/' == $url['path']) {
                 $value = trim($value, '/');
             }
         }
     }
     $length = (int) pods_var(self::$type . '_max_length', $options, 255);
     if (0 < $length && $length < pods_mb_strlen($value)) {
         $value = pods_mb_substr($value, 0, $length);
     }
     return $value;
 }