PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/gmap/lib/Drupal/gmap/GmapMacroToolbox.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016
PHP | 509 lines | 336 code | 41 blank | 132 comment | 86 complexity | 09d9c635be843f27bd9b76c7b04be7bc MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains GmapMacroToolbox.php
  5. *
  6. * former gmap_parse_macro.inc
  7. */
  8. namespace Drupal\gmap;
  9. class GmapMacroToolbox {
  10. /**
  11. * @var static Singleton instance
  12. */
  13. static private $gmapInstance;
  14. /**
  15. * @var array
  16. */
  17. private $style;
  18. /**
  19. * @var string
  20. */
  21. private $coordString;
  22. /**
  23. * @var string
  24. */
  25. private $macroString;
  26. /**
  27. * @var int
  28. */
  29. private $parserVersion;
  30. /**
  31. * Do not change.
  32. */
  33. private function __construct() {
  34. }
  35. /**
  36. * Do not clone.
  37. */
  38. protected function __clone() {
  39. }
  40. /**
  41. * Getting an instance.
  42. *
  43. * @return array
  44. * GmapMacroToolbox SingleTon instance
  45. */
  46. static public function getInstance() {
  47. if (is_null(self::$gmapInstance)) {
  48. self::$gmapInstance = new self();
  49. }
  50. return self::$gmapInstance;
  51. }
  52. /**
  53. * Setting a style.
  54. *
  55. * @param array $style
  56. * Style array.
  57. *
  58. * @return array
  59. * $this GmapMacroToolbox.
  60. *
  61. * former _gmap_parse_style($style)
  62. */
  63. public function setStyle($style) {
  64. $this->style = $style;
  65. return $this;
  66. }
  67. /**
  68. * Getting a parsed styles.
  69. *
  70. * @return array
  71. * former _gmap_parse_style($style)
  72. */
  73. public function getParsedStyles() {
  74. if (strpos($this->style, '/') === FALSE) {
  75. // Style ref.
  76. return $this->style;
  77. }
  78. $styles = explode('/', $this->style);
  79. // @@@ Todo: Fix up old xmaps stuff.
  80. // Possibly detect by looking for array length 7?
  81. // Strip off # signs, they get added by code.
  82. if (isset($styles[0]) && substr($styles[0], 0, 1) == '#') {
  83. $styles[0] = substr($styles[0], 1);
  84. }
  85. if (isset($styles[3]) && substr($styles[3], 0, 1) == '#') {
  86. $styles[3] = substr($styles[3], 1);
  87. }
  88. // Assume anything > 0 and < 1.1 was an old representation.
  89. if ($styles[2] > 0 && $styles[2] < 1.1) {
  90. $styles[2] = $styles[2] * 100;
  91. }
  92. if (isset($styles[4])) {
  93. if ($styles[4] > 0 && $styles[4] < 1.1) {
  94. $styles[4] = $styles[4] * 100;
  95. }
  96. }
  97. return $styles;
  98. }
  99. /**
  100. * Setting $this->coordString.
  101. *
  102. * @param string $str
  103. * Input string variable.
  104. *
  105. * @return object
  106. * $this GmapMacroToolbox
  107. * former _gmap_str2coord($str)
  108. */
  109. public function setCoordString($str) {
  110. $this->coordString = $str;
  111. return $this;
  112. }
  113. /**
  114. * Parse "x.xxxxx, y.yyyyyy (+ x.xxxxx, y.yyyyy ...)" into an array of points.
  115. *
  116. * @return array
  117. * former _gmap_str2coord($str)
  118. */
  119. public function getCoord() {
  120. // Explode along + axis.
  121. $arr = explode('+', $this->coordString);
  122. // Explode along , axis.
  123. $points = array();
  124. foreach ($arr as $pt) {
  125. list($lat, $lon) = explode(',', $pt);
  126. $points[] = array((float) trim($lat), (float) trim($lon));
  127. }
  128. return $points;
  129. }
  130. /**
  131. * Setting $this->macroString and $this->parserVersion params.
  132. *
  133. * @param string $instring
  134. * Input string variable.
  135. *
  136. * @param int $ver
  137. * A version number.
  138. *
  139. * @return object
  140. * former _gmap_parse_macro($instring, $ver = 2)
  141. */
  142. public function setMacroString($instring, $ver = 2) {
  143. $this->macroString = $instring;
  144. $this->parserVersion = $ver;
  145. return $this;
  146. }
  147. /**
  148. * Getting a parsed macro.
  149. *
  150. * @return array
  151. * former _gmap_parse_macro($instring, $ver = 2)
  152. */
  153. public function getParsedMacro() {
  154. // Get a list of keys that are "multiple."
  155. $m = array();
  156. $multiple = gmap_module_invoke('macro_multiple', $m);
  157. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapDefaults.php';
  158. $def = GmapDefaults::getInstance()->getDefaults();
  159. // Remove leading and trailing tags.
  160. if (substr(trim($this->macroString), -1) == ']') {
  161. $this->macroString = substr(trim($this->macroString), 0, -1);
  162. }
  163. if (substr($this->macroString, 0, 5) == '[gmap') {
  164. $this->macroString = substr($this->macroString, 6);
  165. }
  166. // Chop the macro into an array.
  167. $temp = explode('|', $this->macroString);
  168. $m = array();
  169. foreach ($temp as $row) {
  170. $offset = strpos($row, '=');
  171. if ($offset !== FALSE) {
  172. $k = trim(substr($row, 0, $offset));
  173. $r = trim(substr($row, $offset + 1));
  174. if (in_array($k, $multiple)) {
  175. // Things that can appear multiple times.
  176. if (!isset($m[$k])) {
  177. $m[$k] = array();
  178. }
  179. $m[$k][] = $r;
  180. }
  181. else {
  182. $m[$k] = $r;
  183. }
  184. }
  185. }
  186. // Synonyms.
  187. if (isset($m['type'])) {
  188. $m['maptype'] = $m['type'];
  189. unset($m['type']);
  190. }
  191. if (isset($m['control'])) {
  192. $m['controltype'] = $m['control'];
  193. unset($m['control']);
  194. }
  195. if (isset($m['feed']) && is_array($m['feed'])) {
  196. foreach ($m['feed'] as $k => $v) {
  197. $temp = explode('::', $v);
  198. // Normalize url.
  199. if (substr($temp[1], 0, 1) == '/') {
  200. $temp[1] = substr($temp[1], 1);
  201. }
  202. $temp[1] = url($temp[1]);
  203. $m['feed'][$k] = array(
  204. 'markername' => $temp[0],
  205. 'url' => $temp[1],
  206. );
  207. }
  208. }
  209. // Add custom styles.
  210. if (isset($m['style']) && is_array($m['style'])) {
  211. foreach ($m['style'] as $k => $v) {
  212. $temp = explode(':', $v);
  213. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  214. $m['styles'][$temp[0]] = GmapMacroToolbox::getInstance()->setStyle($temp[1])->getParsedStyles();
  215. }
  216. unset($m['style']);
  217. }
  218. // Merge points and markers.
  219. if (!isset($m['points']) || !is_array($m['points'])) {
  220. $m['points'] = array();
  221. }
  222. if (!isset($m['markers']) || !is_array($m['markers'])) {
  223. $m['markers'] = array();
  224. }
  225. $m['markers-temp'] = array_merge($m['points'], $m['markers']);
  226. unset($m['points']);
  227. unset($m['markers']);
  228. // All shapes in 1 array.
  229. if (isset($m['circle']) && is_array($m['circle'])) {
  230. foreach ($m['circle'] as $shape) {
  231. $s = array('type' => 'circle');
  232. $cp = strpos($shape, ':');
  233. if ($cp !== FALSE) {
  234. $stylestr = substr($shape, 0, $cp);
  235. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  236. $s['style'] = GmapMacroToolbox::getInstance()->setStyle($stylestr)->getParsedStyles();
  237. $shape = substr($shape, $cp + 1);
  238. }
  239. $tmp = explode('+', $shape);
  240. $s['radius'] = $tmp[1] ? $tmp[1] : 100;
  241. if (isset($tmp[2]) && $tmp[2]) {
  242. $s['numpoints'] = trim($tmp[2]);
  243. }
  244. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  245. $tmp = GmapMacroToolbox::getInstance()->setCoordString($tmp[0])->getCoord();
  246. $s['center'] = $tmp[0];
  247. $m['shapes'][] = $s;
  248. }
  249. unset($m['circle']);
  250. }
  251. // Fixup legacy lines.
  252. if (isset($m['line1'])) {
  253. if (!isset($m['line'])) {
  254. $m['line'] = array();
  255. }
  256. $m['line'][] = $def['line_colors'][0] . ':' . $m['line1'];
  257. unset($m['line1']);
  258. }
  259. if (isset($m['line2'])) {
  260. if (!isset($m['line'])) {
  261. $m['line'] = array();
  262. }
  263. $m['line'][] = $def['line_colors'][1] . ':' . $m['line3'];
  264. unset($m['line2']);
  265. }
  266. if (isset($m['line3'])) {
  267. if (!isset($m['line'])) {
  268. $m['line'] = array();
  269. }
  270. $m['line'][] = $def['line_colors'][2] . ':' . $m['line3'];
  271. unset($m['line3']);
  272. }
  273. if (isset($m['line']) && is_array($m['line'])) {
  274. foreach ($m['line'] as $shape) {
  275. $s = array('type' => 'line');
  276. $cp = strpos($shape, ':');
  277. if ($cp != FALSE) {
  278. $stylestr = substr($shape, 0, $cp);
  279. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  280. $s['style'] = GmapMacroToolbox::getInstance()->setStyle($stylestr)->getParsedStyles();
  281. $shape = substr($shape, $cp + 1);
  282. }
  283. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  284. $s['points'] = GmapMacroToolbox::getInstance()->setCoordString($shape)->getCoord();
  285. $m['shapes'][] = $s;
  286. }
  287. unset($m['line']);
  288. }
  289. if (isset($m['rpolygon']) && is_array($m['rpolygon'])) {
  290. foreach ($m['rpolygon'] as $shape) {
  291. $s = array('type' => 'rpolygon');
  292. $cp = strpos($shape, ':');
  293. if ($cp !== FALSE) {
  294. $stylestr = substr($shape, 0, $cp);
  295. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  296. $s['style'] = GmapMacroToolbox::getInstance()->setStyle($stylestr)->getParsedStyles();
  297. $shape = substr($shape, $cp + 1);
  298. }
  299. $tmp = explode('+', $shape);
  300. if ($tmp[2]) {
  301. $s['numpoints'] = (int) trim($tmp[2]);
  302. $tmp = array_slice($tmp, 0, 2);
  303. }
  304. $shape = implode('+', $tmp);
  305. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  306. $tmp = GmapMacroToolbox::getInstance()->setCoordString($shape)->getCoord();
  307. $s['center'] = $tmp[0];
  308. $s['point2'] = $tmp[1];
  309. $m['shapes'][] = $s;
  310. }
  311. unset($m['rpolygon']);
  312. }
  313. if (isset($m['polygon']) && is_array($m['polygon'])) {
  314. foreach ($m['polygon'] as $shape) {
  315. $s = array('type' => 'polygon');
  316. $cp = strpos($shape, ':');
  317. if ($cp !== FALSE) {
  318. $stylestr = substr($shape, 0, $cp);
  319. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  320. $s['style'] = GmapMacroToolbox::getInstance()->setStyle($stylestr)->getParsedStyles();
  321. $shape = substr($shape, $cp + 1);
  322. }
  323. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapMacroToolbox.php';
  324. $s['points'] = GmapMacroToolbox::getInstance()->setCoordString($shape)->getCoord();
  325. $m['shapes'][] = $s;
  326. }
  327. unset($m['polygon']);
  328. }
  329. elseif (isset($m['polygon']) && !is_array($m['polygon'])) {
  330. $value = array($m['polygon']);
  331. }
  332. // Version 1 -> 2 conversion.
  333. if ($this->parserVersion == 1) {
  334. // Zoom is flipped.
  335. if (isset($m['zoom'])) {
  336. $m['zoom'] = 18 - $m['zoom'];
  337. if ($m['zoom'] < 1) {
  338. $m['zoom'] = 1;
  339. }
  340. }
  341. }
  342. // Center -> latitude and longitude.
  343. if (isset($m['center']) && $m['center']) {
  344. list($m['latitude'], $m['longitude']) = explode(',', $m['center']);
  345. unset($m['center']);
  346. }
  347. // Behavior.
  348. if (isset($m['behaviour'])) {
  349. $m['behavior'] = $m['behaviour'];
  350. unset($m['behaviour']);
  351. }
  352. if (isset($m['behavior'])) {
  353. $sep = ' ';
  354. if (strpos($m['behavior'], ',') !== FALSE) {
  355. // In some places, commas were used to seperate behaviors.
  356. // This was originally an accident, but it's easy enough to support.
  357. $sep = ',';
  358. }
  359. $m['behavior-temp'] = explode($sep, $m['behavior']);
  360. // 2010 Nov 30 change:
  361. // Fix a very old bug regarding behavior flags:
  362. // It was always supposed to defer to the site default behaviors for every
  363. // flag not defined by the macro, but this was just plain not happening.
  364. // This is a backwards-incompatible change.
  365. $m['behavior'] = $def['behavior'];
  366. foreach ($m['behavior-temp'] as $v) {
  367. $m['behavior'][substr($v, 1)] = (substr($v, 0, 1) == '+') ? TRUE : FALSE;
  368. }
  369. unset($m['behavior-temp']);
  370. }
  371. // Tcontrol now is mtc.
  372. if (isset($m['tcontrol'])) {
  373. if (strtolower(trim($m['tcontrol'])) == 'on') {
  374. $m['mtc'] = 'standard';
  375. }
  376. else {
  377. $m['mtc'] = 'none';
  378. }
  379. unset($m['tcontrol']);
  380. }
  381. // Notype also controls mtc.
  382. if (isset($m['behavior']['notype'])) {
  383. if ($m['behavior']['notype']) {
  384. $m['mtc'] = 'none';
  385. }
  386. unset($m['behavior']['notype']);
  387. }
  388. // Stuff that was converted to behavior flags.
  389. // Scale control.
  390. if (isset($m['scontrol'])) {
  391. if (strtolower(trim($m['scontrol'])) == 'on') {
  392. $m['behavior']['scale'] = TRUE;
  393. }
  394. else {
  395. $m['behavior']['scale'] = FALSE;
  396. }
  397. unset($m['scontrol']);
  398. }
  399. // Draggability.
  400. if (isset($m['drag'])) {
  401. if (strtolower(trim($m['drag'])) == 'yes') {
  402. $m['behavior']['nodrag'] = FALSE;
  403. }
  404. else {
  405. $m['behavior']['nodrag'] = TRUE;
  406. }
  407. unset($m['drag']);
  408. }
  409. // Markers fixup.
  410. foreach ($m['markers-temp'] as $t) {
  411. unset($markername);
  412. // Named?
  413. // Single : gets handled below.
  414. if (strpos($t, '::')) {
  415. list($markername, $t) = explode('::', $t, 2);
  416. }
  417. // Break down into points.
  418. $points = explode('+', $t);
  419. $offset = -1;
  420. foreach ($points as $point) {
  421. $marker = array();
  422. $offset++;
  423. $marker['options'] = array();
  424. // Labelled?
  425. // @@@ Gmap allows both a tooltip and a popup, how to represent?
  426. if (strpos($point, ':')) {
  427. list($point, $marker['text']) = explode(':', $point, 2);
  428. $marker['text'] = theme('gmap_marker_popup', array('label' => $marker['text']));
  429. }
  430. if (strpos($point, '%')) {
  431. list($point, $addons) = explode('%', $point, 2);
  432. $motemp = explode('%', $addons);
  433. foreach ($motemp as $option) {
  434. $marker['options'][trim($option)] = TRUE;
  435. }
  436. }
  437. list($marker['latitude'], $marker['longitude']) = explode(',', $point, 2);
  438. // Named markers get an offset too.
  439. if (isset($markername)) {
  440. $marker['markername'] = $markername;
  441. $marker['offset'] = $offset;
  442. }
  443. $m['markers'][] = $marker;
  444. }
  445. }
  446. unset($m['markers-temp']);
  447. // Assign an id if one wasn't specified.
  448. if (!isset($m['id'])) {
  449. $m['id'] = gmap_get_auto_mapid();
  450. }
  451. // The macro can now be manipulated by reference.
  452. // Note: We do NOT use gmap_module_invoke here,
  453. // as this $op has weird semantics for backwards
  454. // compatibility / convenience reasons.
  455. // (Specifically, modules are allowed to do arbitrary
  456. // manipulations on $m OR return the changes
  457. // they want to apply to $m.)
  458. foreach (module_implements('gmap') as $module) {
  459. $additions = call_user_func_array($module . '_gmap', array('parse_macro', &$m));
  460. if (!empty($additions)) {
  461. foreach ($additions as $k => $v) {
  462. $m[$k] = $v;
  463. }
  464. }
  465. }
  466. return $m;
  467. }
  468. }