/exchange/code/trunk/administrator/components/com_exchange/helpers/migrate.php
PHP | 157 lines | 122 code | 8 blank | 27 comment | 6 complexity | b6777df79c32b32e6c5995e5b4c4cd90 MD5 | raw file
- <?php
- /**
- * @version $Id: migrate.php 280 2010-09-18 02:14:15Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- jximport('jxtended.database.query');
- /**
- * Accepts a string in the dms format (e.g., "S33.50'23.85") and returns the
- * equivalent degrees value (i.e., 33 + (50/60) + 23.85/3600 = -33.8399583333).
- */
- function dms2deg($value)
- {
- if ($value != '')
- {
- $first = substr($value, 0, 1);
- $rest = substr($value, 1);
- $split = preg_split("/'/", $rest);
- $seconds = $split[1];
- $degreesminutes = preg_split("/\./", $split[0]);
- $degrees = $degreesminutes[0];
- $minutes = $degreesminutes[1];
- $degreesValue = $degrees + ($minutes / 60) + ($seconds / 3600);
- // if the first character is 'S' or 'W' the degrees value is negative
- if (($first == 'S') || ($first == 'W')) {
- $degreesValue = -$degreesValue;
- }
- return $degreesValue;
- }
- else
- {
- return null;
- }
- }
- /**
- * Looks up the id matching the given value in the specified field and table.
- * @param string the value to look up.
- * @param string the table to look up.
- * @param string the field to look up.
- * @param array Additional where conditions to apply to the query clause
- * @param array Additional options for the lookup
- * @return int the id matching the look up. null is returned if the value
- * is an empty string or the value is not found in the
- * specified table.
- */
- function lookupId($value, $tableName, $fieldName, $conditions = array(), $options = array())
- {
- return lookup('id', $value, $tableName, $fieldName, $conditions, $options);
- }
- /**
- * Looks up the field matching the given value in the specified field and table.
- * @param string the lookup field
- * @param string the value to look up.
- * @param string the table to look up.
- * @param string the field to look up.
- * @param array Additional where conditions to apply to the query clause
- * @param array Additional options for the lookup
- * @return int the id matching the look up. null is returned if the value
- * is an empty string or the value is not found in the
- * specified table.
- */
- function lookup($field, $value, $tableName, $fieldName, $conditions = array(), $options = array())
- {
- static $saved;
- $key = md5($value.$tableName.$fieldName.serialize($conditions));
- if (isset($saved[$key])) {
- return $saved[$key];
- }
- else
- {
- if ($value != '')
- {
- $db = JFactory::getDBO();
- $cleanValue = trim($value);
- $tableName = $db->getEscaped($tableName);
- $fieldName = $db->getEscaped($fieldName);
- $query = new JXQuery;
- $query->select($field);
- // Special attention to the from table
- // Sometimes we want to look up the "from" table and it has a different prefix
- if (isset($options['fromNoPrefix'])) {
- $query->from($tableName);
- }
- else {
- $query->from('#__'.$tableName);
- }
- $query->where($fieldName.' = '.$db->Quote($cleanValue));
- foreach ($conditions as $f => $v) {
- if (is_integer($f)) {
- JError::raiseWarning(500, 'Condition has int key for value ='.$v.'. Check you have passed a named array');
- }
- else {
- $query->where($db->NameQuote($f).' = '.$db->Quote($v));
- }
- }
- $db->setQuery($query->toString());
- $data = $db->loadResult();
- if ($msg = $db->getErrorMsg()) {
- JError::raiseWarning(500, $msg);
- }
- //ExchangeLog::add(200, '<fieldset>'.$db->getQuery().'<hr />'.$data.'</fieldset>');
- $saved[$key] = $data;
- return $data;
- }
- else {
- return null;
- }
- }
- }
- /**
- * @param string The field name
- * @return strong A clean name
- */
- function cleanFieldNames(&$fields)
- {
- // clean up field names (spaces to underscores)
- foreach ($fields as $k => $v)
- {
- $v = trim($v);
- $v = str_replace(' ', '_', $v);
- $v = preg_replace('#\W#', '', $v);
- // this line is messing up the data
- // $v = preg_replace('#[A-Z0-9_]#i', '', $v);
- $fields[$k] = strtolower($v);
- }
- }
- /**
- * Makes an alias style value
- *
- * @param string
- * @return string
- */
- function makeAlias($text)
- {
- $text = preg_replace('#[\s\-]+#', '-', $text);
- $text = preg_replace('#\&#i', 'and', $text);
- $text = preg_replace('#[^A-Z0-9\-\_]#i', '', $text);
- return strtolower($text);
- }