123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- class Utils {
- public static function array_to_json_string($arraydata) {
- $output = "";
- $output .= "{";
- $output1 = array();
- foreach($arraydata as $key => $val) {
- if (is_array($val)) {
- $output2 = array();
- foreach($val as $subkey => $subval) {
- if (is_array($subval)) {
- $output3 = array();
- foreach ($subval as $subsubkey => $subsubval) {
- $output3[] = "\"" . $subsubkey . "\" : \"" . $subsubval . "\"";
- }
- $output2[] = "\"" . $subkey . "\" : [{" . implode(',', $output3) . "}]";
- } else {
- $output2[] = "\"" . $subkey . "\" : \"" . $subval . "\"";
- }
- }
- $output1[] = "\"" . $key . "\" : [{" . implode(',', $output2) . "}]";
- } else {
- $output1[] = "\"" . $key . "\" : \"" . $val . "\"";
- }
- }
- $output .= implode(',', $output1);
- $output .= "}";
- return $output;
- }
- public static function array_to_json($array) {
- if (!is_array($array)) {
- return false;
- }
- $associative = count(array_diff(array_keys($array), array_keys(array_keys($array))));
- if ($associative) {
- $construct = array();
- foreach($array as $key => $value) {
- // We first copy each key/value pair into a staging array,
- // formatting each key and value properly as we go.
- // Format the key:
- if (is_numeric($key)) {
- $key = "key_$key";
- }
- $key = "'" . addslashes($key) . "'";
- // Format the value:
- if (is_array($value)) {
- $value = array_to_json($value);
- } else if (!is_numeric($value) || is_string($value)) {
- $value = "'" . addslashes($value) . "'";
- }
- // Add to staging array:
- $construct[] = "$key: $value";
- }
- // Then we collapse the staging array into the JSON form:
- $result = "{ " . implode(", ", $construct) . " }";
- } else { // If the array is a vector (not associative):
- $construct = array();
- foreach($array as $value) {
- // Format the value:
- if (is_array($value)) {
- $value = array_to_json($value);
- } else if (!is_numeric($value) || is_string($value)) {
- $value = "'" . addslashes($value) . "'";
- }
- // Add to staging array:
- $construct[] = $value;
- }
- // Then we collapse the staging array into the JSON form:
- $result = "[ " . implode(", ", $construct) . " ]";
- }
- return $result;
- }
- static public function parseOrderedData($data) {
- $final = array();
- if ('' != $data) {
- $containers = explode(":", $data);
- foreach($containers as $container) {
- $container = str_replace(")", "", $container);
- $i = 0;
- $lastly = explode("(", $container);
- $values = explode(",", $lastly[1]);
- foreach($values AS $value) {
- if ($value == '') {
- continue;
- }
- $final[$lastly[0]][] = $value;
- $i ++;
- }
- }
- }
- return $final;
- }
- static public function sanitizeSupplierId($supplier_id) {
- $supplier_id = str_replace('supplier-', '', $supplier_id);
- $supplier_id = str_replace('all-products-', '', $supplier_id);
- $supplier_id = str_replace('products-', '', $supplier_id);
- return (int)$supplier_id;
- }
- /**
- * Utils::formatCurrency()
- *
- * @param float $value
- * @return string
- */
- public static function formatCurrency($value, $decimals = 2) {
- return sprintf('%s', number_format(round($value, 2), $decimals, ',', ' '));
- }
- /**
- * Utils::formatCurrencyEuro()
- *
- * @param float $value
- * @return string
- */
- public static function formatCurrencyEuro($value, $decimals = 2) {
- return self::formatCurrency(round($value, 2), $decimals) . ' €';
- }
- /**
- * Utils::formatCurrencySol()
- *
- * @param float $value
- * @return string
- */
- public static function formatCurrencySol($value, $decimals = 2) {
- return sprintf('%d %s', round($value, 2), wpConfig::getAdditionalCurrencyName());
- }
- /**
- * Utils::formatCurrencyFranc()
- *
- * @param float $value
- * @return string
- */
- public static function formatCurrencyFranc($value, $decimals = 2) {
- return sprintf('%s FF', number_format(round(round($value, 2) * 6.55957, 2), $decimals, ',', ' '));
- }
- public static function getKataoStartDate($katao_node_id = false) {
- return CatalyzDate::formatShort(KataoNodePeer::getBeginDate($katao_node_id));
- }
- public static function getKataoMembersCount($katao_node_id = false) {
- $criteria = new Criteria();
- $criteria->addJoin(KataoMemberPeer::ID, KataoUserPeer::KATAO_MEMBER_ID);
- $criteria->add(KataoUserPeer::STATUS, KataoUser::STATUS_ACTIVE);
- if ($katao_node_id) {
- $criteria->add(KataoMemberPeer::KATAO_NODE_ID, $katao_node_id);
- }
- return number_format(KataoMemberPeer::doCount($criteria), 0, ',', ' ');
- }
- public static function getKataoTransactionsCount($katao_node_id = false, $year = false) {
- $return = 0;
- if (!$year && (!$katao_node_id || KataoNode::ID_MONTAUBAN == $katao_node_id)) {
- $return += wpConfig::getInitialTransactionsCount();
- }
- $return += KataoInvoicePeer::addAll($katao_node_id, $year);
- return number_format($return, 0, ',', ' ');
- }
- public static function getKataoTransactionsSum($katao_node_id = false, $year = false) {
- $return = 0;
- if (!$year && (!$katao_node_id || KataoNode::ID_MONTAUBAN == $katao_node_id)) {
- $return += wpConfig::getInitialTransactionsSum();
- }
- $return += KataoInvoicePeer::sumAll($katao_node_id, $year);
- return self::formatCurrencyEuro($return, 0);
- }
- public static function getGmapLatLng($address1, $address2) {
- $return = array();
- $address1 = trim($address1);
- $address2 = trim($address2);
- $return = Utils::getLatLng($address1);
- if (empty($return['lat']) && empty($return['lng'])) {
- list($lat, $long) = Utils::getLatLng($address2);
- $return['lat'] = $lat;
- $return['lng'] = $long;
- }
- return $return;
- }
- public static function getLatLng($address) {
- $xml = simplexml_load_string(utf8_encode(file_get_contents(sprintf('https://maps.googleapis.com/maps/api/geocode/xml?address=%s&sensor=false&key='.sfConfig::get('app_gmap'), urlencode($address)))));
- if ($xml->status == 'OK') {
- // Adresse correcte
- $coordinates = $xml->result->geometry->location;
- return array('lat' => (string) $coordinates->lat, 'lng' => (string) $coordinates->lng);
- }
- /*elseif (601 == $code) {
- // // Adresse manquante
- // if ('' != $type) {
- // // print_r(sprintf("\n" . '%s: Adresse "%s" complète inconnue pour "%s"', $type, $address, $name));
- // }
- // return false;
- // } elseif (602 == $code) {
- // // // Adresse invalide
- // // set_time_limit(60);
- // // $address = trim($object->getVille());
- // // $xml = simplexml_load_string(utf8_encode(file_get_contents(sprintf('https://maps.google.com/maps/geo?q=%s&output=xml&key=%s', urlencode($address), sfConfig::get('app_sfGMaps_key')))));
- // // $code = (int)$xml->Response->Status->code;
- // // if (200 == $code) {
- // // // Adresse correcte
- // // $coordinates = $xml->Response->Placemark->Point->coordinates;
- // // list($lng, $lat, $altitude) = explode(',', $coordinates);
- // // $object->setLatitude($lat);
- // // $object->setLongitude($lng);
- // // $object->save();
- // // } elseif (601 == $code) {
- // // // Adresse manquante
- // // if ('' != $type) {
- // // print_r(sprintf("\n" . '%s: Ville "%s" inconnue pour "%s"', $type, $address, $name));
- // // }
- // // } elseif (602 == $code) {
- // // // Adresse invalide
- // // if ('' != $type) {
- // // print_r(sprintf("\n" . '%s: Ville "%s" invalide pour "%s"', $type, $address, $name));
- // // }
- // // }
- // }
- */
- return false;
- }
- public static function formatSiretNmber($value) {
- return preg_replace('/\D/si', '', $value);
- }
- public static function between($value, $min, $max, $strict = false) {
- if ($strict) {
- return $value > $min && $value < $max;
- }
- return $value >= $min && $value <= $max;
- }
- public static function getKataoEmailFooter() {
- return sprintf('
- Bien à toi,
- L\'équipe Katao
- %s
- %s', wpConfig::getEmail(), wpConfig::getPhone());
- }
- public static function completeWithSpaces($string, $number, $left = false) {
- $string = trim($string);
- if (strlen($string) > $number) {
- $string = substr($string, 0, $number);
- } else {
- $repeat = $number - strlen($string);
- for($i = 0; $i < $repeat; $i++) {
- if ($left) {
- $string = ' ' . $string;
- } else {
- $string .= ' ';
- }
- }
- }
- return $string;
- }
- public static function subscribeToMailingList($email) {
- try {
- if ('' != $mailing_list_email = wpConfig::getMailingListEmail()) {
- $mailer = new wpMail();
- $recipients = new Swift_RecipientList();
- $recipients->addTo($mailing_list_email . '+subscribe@googlegroups.com');
- $recipients->addBcc('frederic.bosque@freemind.fr');
- $mailer->send(new Swift_Message(), $recipients, $email);
- $mailer->disconnect();
- }
- }
- catch (Exception $e) {
- $mailer->disconnect();
- }
- }
- public static function removeAccents($string) {
- return strtr($string, utf8_decode('ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ'), 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn');
- }
- public static function getInputDateTagOptions() {
- return array('size' => 8, 'withtime' => false, 'rich' => true, 'calendar_button_img' => '/sf/sf_admin/images/date.png', 'css' => 'skins/aqua/theme', 'format' => 'dd/MM/yyyy', 'culture' => 'fr');
- }
- public static function getDateFromInput($value) {
- $return = time();
- if (preg_match('|(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})|si', $value, $matches)) {
- $return = mktime(0, 0, 0, (int)$matches['month'], (int)$matches['day'], $matches['year']);
- }
- return $return;
- }
- }
- ?>
|