PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/exchange/code/trunk/administrator/components/com_exchange/helpers/migrate.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 157 lines | 122 code | 8 blank | 27 comment | 6 complexity | b6777df79c32b32e6c5995e5b4c4cd90 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: migrate.php 280 2010-09-18 02:14:15Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_exchange
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. jximport('jxtended.database.query');
  13. /**
  14. * Accepts a string in the dms format (e.g., "S33.50'23.85") and returns the
  15. * equivalent degrees value (i.e., 33 + (50/60) + 23.85/3600 = -33.8399583333).
  16. */
  17. function dms2deg($value)
  18. {
  19. if ($value != '')
  20. {
  21. $first = substr($value, 0, 1);
  22. $rest = substr($value, 1);
  23. $split = preg_split("/'/", $rest);
  24. $seconds = $split[1];
  25. $degreesminutes = preg_split("/\./", $split[0]);
  26. $degrees = $degreesminutes[0];
  27. $minutes = $degreesminutes[1];
  28. $degreesValue = $degrees + ($minutes / 60) + ($seconds / 3600);
  29. // if the first character is 'S' or 'W' the degrees value is negative
  30. if (($first == 'S') || ($first == 'W')) {
  31. $degreesValue = -$degreesValue;
  32. }
  33. return $degreesValue;
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. /**
  41. * Looks up the id matching the given value in the specified field and table.
  42. * @param string the value to look up.
  43. * @param string the table to look up.
  44. * @param string the field to look up.
  45. * @param array Additional where conditions to apply to the query clause
  46. * @param array Additional options for the lookup
  47. * @return int the id matching the look up. null is returned if the value
  48. * is an empty string or the value is not found in the
  49. * specified table.
  50. */
  51. function lookupId($value, $tableName, $fieldName, $conditions = array(), $options = array())
  52. {
  53. return lookup('id', $value, $tableName, $fieldName, $conditions, $options);
  54. }
  55. /**
  56. * Looks up the field matching the given value in the specified field and table.
  57. * @param string the lookup field
  58. * @param string the value to look up.
  59. * @param string the table to look up.
  60. * @param string the field to look up.
  61. * @param array Additional where conditions to apply to the query clause
  62. * @param array Additional options for the lookup
  63. * @return int the id matching the look up. null is returned if the value
  64. * is an empty string or the value is not found in the
  65. * specified table.
  66. */
  67. function lookup($field, $value, $tableName, $fieldName, $conditions = array(), $options = array())
  68. {
  69. static $saved;
  70. $key = md5($value.$tableName.$fieldName.serialize($conditions));
  71. if (isset($saved[$key])) {
  72. return $saved[$key];
  73. }
  74. else
  75. {
  76. if ($value != '')
  77. {
  78. $db = JFactory::getDBO();
  79. $cleanValue = trim($value);
  80. $tableName = $db->getEscaped($tableName);
  81. $fieldName = $db->getEscaped($fieldName);
  82. $query = new JXQuery;
  83. $query->select($field);
  84. // Special attention to the from table
  85. // Sometimes we want to look up the "from" table and it has a different prefix
  86. if (isset($options['fromNoPrefix'])) {
  87. $query->from($tableName);
  88. }
  89. else {
  90. $query->from('#__'.$tableName);
  91. }
  92. $query->where($fieldName.' = '.$db->Quote($cleanValue));
  93. foreach ($conditions as $f => $v) {
  94. if (is_integer($f)) {
  95. JError::raiseWarning(500, 'Condition has int key for value ='.$v.'. Check you have passed a named array');
  96. }
  97. else {
  98. $query->where($db->NameQuote($f).' = '.$db->Quote($v));
  99. }
  100. }
  101. $db->setQuery($query->toString());
  102. $data = $db->loadResult();
  103. if ($msg = $db->getErrorMsg()) {
  104. JError::raiseWarning(500, $msg);
  105. }
  106. //ExchangeLog::add(200, '<fieldset>'.$db->getQuery().'<hr />'.$data.'</fieldset>');
  107. $saved[$key] = $data;
  108. return $data;
  109. }
  110. else {
  111. return null;
  112. }
  113. }
  114. }
  115. /**
  116. * @param string The field name
  117. * @return strong A clean name
  118. */
  119. function cleanFieldNames(&$fields)
  120. {
  121. // clean up field names (spaces to underscores)
  122. foreach ($fields as $k => $v)
  123. {
  124. $v = trim($v);
  125. $v = str_replace(' ', '_', $v);
  126. $v = preg_replace('#\W#', '', $v);
  127. // this line is messing up the data
  128. // $v = preg_replace('#[A-Z0-9_]#i', '', $v);
  129. $fields[$k] = strtolower($v);
  130. }
  131. }
  132. /**
  133. * Makes an alias style value
  134. *
  135. * @param string
  136. * @return string
  137. */
  138. function makeAlias($text)
  139. {
  140. $text = preg_replace('#[\s\-]+#', '-', $text);
  141. $text = preg_replace('#\&#i', 'and', $text);
  142. $text = preg_replace('#[^A-Z0-9\-\_]#i', '', $text);
  143. return strtolower($text);
  144. }