utils.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. class Utils {
  3. public static function array_to_json_string($arraydata) {
  4. $output = "";
  5. $output .= "{";
  6. $output1 = array();
  7. foreach($arraydata as $key => $val) {
  8. if (is_array($val)) {
  9. $output2 = array();
  10. foreach($val as $subkey => $subval) {
  11. if (is_array($subval)) {
  12. $output3 = array();
  13. foreach ($subval as $subsubkey => $subsubval) {
  14. $output3[] = "\"" . $subsubkey . "\" : \"" . $subsubval . "\"";
  15. }
  16. $output2[] = "\"" . $subkey . "\" : [{" . implode(',', $output3) . "}]";
  17. } else {
  18. $output2[] = "\"" . $subkey . "\" : \"" . $subval . "\"";
  19. }
  20. }
  21. $output1[] = "\"" . $key . "\" : [{" . implode(',', $output2) . "}]";
  22. } else {
  23. $output1[] = "\"" . $key . "\" : \"" . $val . "\"";
  24. }
  25. }
  26. $output .= implode(',', $output1);
  27. $output .= "}";
  28. return $output;
  29. }
  30. public static function array_to_json($array) {
  31. if (!is_array($array)) {
  32. return false;
  33. }
  34. $associative = count(array_diff(array_keys($array), array_keys(array_keys($array))));
  35. if ($associative) {
  36. $construct = array();
  37. foreach($array as $key => $value) {
  38. // We first copy each key/value pair into a staging array,
  39. // formatting each key and value properly as we go.
  40. // Format the key:
  41. if (is_numeric($key)) {
  42. $key = "key_$key";
  43. }
  44. $key = "'" . addslashes($key) . "'";
  45. // Format the value:
  46. if (is_array($value)) {
  47. $value = array_to_json($value);
  48. } else if (!is_numeric($value) || is_string($value)) {
  49. $value = "'" . addslashes($value) . "'";
  50. }
  51. // Add to staging array:
  52. $construct[] = "$key: $value";
  53. }
  54. // Then we collapse the staging array into the JSON form:
  55. $result = "{ " . implode(", ", $construct) . " }";
  56. } else { // If the array is a vector (not associative):
  57. $construct = array();
  58. foreach($array as $value) {
  59. // Format the value:
  60. if (is_array($value)) {
  61. $value = array_to_json($value);
  62. } else if (!is_numeric($value) || is_string($value)) {
  63. $value = "'" . addslashes($value) . "'";
  64. }
  65. // Add to staging array:
  66. $construct[] = $value;
  67. }
  68. // Then we collapse the staging array into the JSON form:
  69. $result = "[ " . implode(", ", $construct) . " ]";
  70. }
  71. return $result;
  72. }
  73. static public function parseOrderedData($data) {
  74. $final = array();
  75. if ('' != $data) {
  76. $containers = explode(":", $data);
  77. foreach($containers as $container) {
  78. $container = str_replace(")", "", $container);
  79. $i = 0;
  80. $lastly = explode("(", $container);
  81. $values = explode(",", $lastly[1]);
  82. foreach($values AS $value) {
  83. if ($value == '') {
  84. continue;
  85. }
  86. $final[$lastly[0]][] = $value;
  87. $i ++;
  88. }
  89. }
  90. }
  91. return $final;
  92. }
  93. static public function sanitizeSupplierId($supplier_id) {
  94. $supplier_id = str_replace('supplier-', '', $supplier_id);
  95. $supplier_id = str_replace('all-products-', '', $supplier_id);
  96. $supplier_id = str_replace('products-', '', $supplier_id);
  97. return (int)$supplier_id;
  98. }
  99. /**
  100. * Utils::formatCurrency()
  101. *
  102. * @param float $value
  103. * @return string
  104. */
  105. public static function formatCurrency($value, $decimals = 2) {
  106. return sprintf('%s', number_format(round($value, 2), $decimals, ',', ' '));
  107. }
  108. /**
  109. * Utils::formatCurrencyEuro()
  110. *
  111. * @param float $value
  112. * @return string
  113. */
  114. public static function formatCurrencyEuro($value, $decimals = 2) {
  115. return self::formatCurrency(round($value, 2), $decimals) . ' €';
  116. }
  117. /**
  118. * Utils::formatCurrencySol()
  119. *
  120. * @param float $value
  121. * @return string
  122. */
  123. public static function formatCurrencySol($value, $decimals = 2) {
  124. return sprintf('%d %s', round($value, 2), wpConfig::getAdditionalCurrencyName());
  125. }
  126. /**
  127. * Utils::formatCurrencyFranc()
  128. *
  129. * @param float $value
  130. * @return string
  131. */
  132. public static function formatCurrencyFranc($value, $decimals = 2) {
  133. return sprintf('%s FF', number_format(round(round($value, 2) * 6.55957, 2), $decimals, ',', ' '));
  134. }
  135. public static function getKataoStartDate($katao_node_id = false) {
  136. return CatalyzDate::formatShort(KataoNodePeer::getBeginDate($katao_node_id));
  137. }
  138. public static function getKataoMembersCount($katao_node_id = false) {
  139. $criteria = new Criteria();
  140. $criteria->addJoin(KataoMemberPeer::ID, KataoUserPeer::KATAO_MEMBER_ID);
  141. $criteria->add(KataoUserPeer::STATUS, KataoUser::STATUS_ACTIVE);
  142. if ($katao_node_id) {
  143. $criteria->add(KataoMemberPeer::KATAO_NODE_ID, $katao_node_id);
  144. }
  145. return number_format(KataoMemberPeer::doCount($criteria), 0, ',', ' ');
  146. }
  147. public static function getKataoTransactionsCount($katao_node_id = false, $year = false) {
  148. $return = 0;
  149. if (!$year && (!$katao_node_id || KataoNode::ID_MONTAUBAN == $katao_node_id)) {
  150. $return += wpConfig::getInitialTransactionsCount();
  151. }
  152. $return += KataoInvoicePeer::addAll($katao_node_id, $year);
  153. return number_format($return, 0, ',', ' ');
  154. }
  155. public static function getKataoTransactionsSum($katao_node_id = false, $year = false) {
  156. $return = 0;
  157. if (!$year && (!$katao_node_id || KataoNode::ID_MONTAUBAN == $katao_node_id)) {
  158. $return += wpConfig::getInitialTransactionsSum();
  159. }
  160. $return += KataoInvoicePeer::sumAll($katao_node_id, $year);
  161. return self::formatCurrencyEuro($return, 0);
  162. }
  163. public static function getGmapLatLng($address1, $address2) {
  164. $return = array();
  165. $address1 = trim($address1);
  166. $address2 = trim($address2);
  167. $return = Utils::getLatLng($address1);
  168. if (empty($return['lat']) && empty($return['lng'])) {
  169. list($lat, $long) = Utils::getLatLng($address2);
  170. $return['lat'] = $lat;
  171. $return['lng'] = $long;
  172. }
  173. return $return;
  174. }
  175. public static function getLatLng($address) {
  176. $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)))));
  177. if ($xml->status == 'OK') {
  178. // Adresse correcte
  179. $coordinates = $xml->result->geometry->location;
  180. return array('lat' => (string) $coordinates->lat, 'lng' => (string) $coordinates->lng);
  181. }
  182. /*elseif (601 == $code) {
  183. // // Adresse manquante
  184. // if ('' != $type) {
  185. // // print_r(sprintf("\n" . '%s: Adresse "%s" complète inconnue pour "%s"', $type, $address, $name));
  186. // }
  187. // return false;
  188. // } elseif (602 == $code) {
  189. // // // Adresse invalide
  190. // // set_time_limit(60);
  191. // // $address = trim($object->getVille());
  192. // // $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')))));
  193. // // $code = (int)$xml->Response->Status->code;
  194. // // if (200 == $code) {
  195. // // // Adresse correcte
  196. // // $coordinates = $xml->Response->Placemark->Point->coordinates;
  197. // // list($lng, $lat, $altitude) = explode(',', $coordinates);
  198. // // $object->setLatitude($lat);
  199. // // $object->setLongitude($lng);
  200. // // $object->save();
  201. // // } elseif (601 == $code) {
  202. // // // Adresse manquante
  203. // // if ('' != $type) {
  204. // // print_r(sprintf("\n" . '%s: Ville "%s" inconnue pour "%s"', $type, $address, $name));
  205. // // }
  206. // // } elseif (602 == $code) {
  207. // // // Adresse invalide
  208. // // if ('' != $type) {
  209. // // print_r(sprintf("\n" . '%s: Ville "%s" invalide pour "%s"', $type, $address, $name));
  210. // // }
  211. // // }
  212. // }
  213. */
  214. return false;
  215. }
  216. public static function formatSiretNmber($value) {
  217. return preg_replace('/\D/si', '', $value);
  218. }
  219. public static function between($value, $min, $max, $strict = false) {
  220. if ($strict) {
  221. return $value > $min && $value < $max;
  222. }
  223. return $value >= $min && $value <= $max;
  224. }
  225. public static function getKataoEmailFooter() {
  226. return sprintf('
  227. Bien à toi,
  228. L\'équipe Katao
  229. %s
  230. %s', wpConfig::getEmail(), wpConfig::getPhone());
  231. }
  232. public static function completeWithSpaces($string, $number, $left = false) {
  233. $string = trim($string);
  234. if (strlen($string) > $number) {
  235. $string = substr($string, 0, $number);
  236. } else {
  237. $repeat = $number - strlen($string);
  238. for($i = 0; $i < $repeat; $i++) {
  239. if ($left) {
  240. $string = ' ' . $string;
  241. } else {
  242. $string .= ' ';
  243. }
  244. }
  245. }
  246. return $string;
  247. }
  248. public static function subscribeToMailingList($email) {
  249. try {
  250. if ('' != $mailing_list_email = wpConfig::getMailingListEmail()) {
  251. $mailer = new wpMail();
  252. $recipients = new Swift_RecipientList();
  253. $recipients->addTo($mailing_list_email . '+subscribe@googlegroups.com');
  254. $recipients->addBcc('frederic.bosque@freemind.fr');
  255. $mailer->send(new Swift_Message(), $recipients, $email);
  256. $mailer->disconnect();
  257. }
  258. }
  259. catch (Exception $e) {
  260. $mailer->disconnect();
  261. }
  262. }
  263. public static function removeAccents($string) {
  264. return strtr($string, utf8_decode('ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ'), 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn');
  265. }
  266. public static function getInputDateTagOptions() {
  267. 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');
  268. }
  269. public static function getDateFromInput($value) {
  270. $return = time();
  271. if (preg_match('|(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})|si', $value, $matches)) {
  272. $return = mktime(0, 0, 0, (int)$matches['month'], (int)$matches['day'], $matches['year']);
  273. }
  274. return $return;
  275. }
  276. }
  277. ?>