/** * Property writing accessor * @param string $propertyName The property name * @param string $value The property value */ public function setProperty($propertyName, $value) { // Non-null value if ($value !== NULL) { // Numeric value if (StringTool::isInt($value)) { $value = StringTool::toInt($value, false); } else { if (StringTool::isFloat($value, FALSE)) { $value = StringTool::toFloat($value, false); } else { if (StringTool::endsWith($propertyName, 'date')) { // Date has a 10 length (YYYY-mm-dd) if (StringTool::strlen($value) == 10) { $value = DateTool::stringToTimestamp($value, DateTool::FORMAT_MYSQL_DATE); } else { $value = DateTool::stringToTimestamp($value); } } } } // Day property type } // Removes table name at the beginning of field name, not for id fields nor xxx_has_xxx tables $tableName = DatabaseFactory::getElementTableName($this->getElementClass()); if (!StringTool::contains($tableName, ElementFactory::TABLE_JOIN_SEPARATOR)) { $tablePrefix = $tableName . '_'; $tableIdField = $tablePrefix . 'id'; if (StringTool::startsWith($propertyName, $tablePrefix) && (!StringTool::endsWith($propertyName, '_id') || $propertyName == $tableIdField)) { $propertyName = StringTool::truncateFirstChars($propertyName, StringTool::strlen($tablePrefix)); } } // Updates original property list if (!ArrayTool::array_key_exists($propertyName, $this->propertyList)) { // It's the first time this property gets a value, it will be updated (set a null value to original property list) $this->originalPropertyList[$propertyName] = NULL; } else { if (ArrayTool::array_key_exists($propertyName, $this->originalPropertyList)) { // Attribute value had already changed (originalPropertyList already has a value for this property) // If value has been reset to original value, removes the update of the property if ($value == $this->originalPropertyList[$propertyName]) { unset($this->originalPropertyList[$propertyName]); } } else { if ($value !== $this->propertyList[$propertyName]) { // If value has changed, updates original value $this->originalPropertyList[$propertyName] = $this->propertyList[$propertyName]; } } } // Sets property new value $this->propertyList[$propertyName] = $value; }
/** * Imports data, creating a table and inserting rows * @param string $tablename The destination table's name * @param array $head The column headers * @param array $data The data to import * @param string $mismatchtable Optional table's name to store type mismatch alerts * @return boolean TRUE on success or FALSE on failure */ public function importRequest($tablename = null, $head = null, $data = null, $mismatchtable = null) { if ($tablename === null || $head === null || $data === null) { // mandatory params return false; } try { $logInstance = LogTool::getInstance(); $coltype = array(); // tells int, float or varchar for each column $nadata = array(); // stores type mismatch alerts in a second table $nb_lines_to_check = 5; $sql = 'CREATE TABLE IF NOT EXISTS ' . $tablename . ' ('; if ($mismatchtable) { $sql2 = 'CREATE TABLE IF NOT EXISTS ' . $mismatchtable . ' ('; } $nb_col_to_check = array_key_exists(0, $head) ? count($head) - 1 : count($head); // if columns begin at #1, there's no id and we won't create one ; // if they begin at #0, it probably means there's an id, so let's handle it if (array_key_exists(0, $head)) { // " && $head[0] == 'id' " unnecessary $coltype[$head[0]] = 'int(11)'; $sql .= '`' . $head[0] . '` ' . $coltype[$head[0]] . ', '; } for ($i = 1; $i <= $nb_col_to_check; $i++) { $colindex = StringTool::cleanVarName($head[$i]); if ($colindex == SGBD_SDF_MOLSTRUCCOL) { $coltype[$colindex] = 'text'; } else { $nb_flt = 0; $nb_int = 0; $nb_str = 0; // try the first $nb_lines_to_check lines of data for ($j = 0; $j < $nb_lines_to_check; $j++) { //$logInstance->logDebug("j : ".$j." - colindex : ".$colindex); if (array_key_exists($j, $data)) { // "false" if less than 5 lines in data $temp = $data[$j][$colindex]; if (StringTool::isFloat($temp)) { $nb_flt++; } elseif (StringTool::isInt($temp)) { $nb_int++; } else { $nb_str++; } } } if ($nb_flt > 0 && $nb_flt + $nb_int >= $nb_lines_to_check - 1) { // we tolerate 1 line with wrong type $coltype[$colindex] = 'float'; } elseif ($nb_int >= $nb_lines_to_check - 1) { $coltype[$colindex] = 'int(11)'; } else { $coltype[$colindex] = 'text'; } // varchar too short sometimes if ($mismatchtable) { $sql2 .= '`' . $head[$i] . '` varchar(50), '; } // store mismatches directly in this table (not just 0/1/null) } $sql .= '`' . $head[$i] . '` ' . $coltype[$colindex] . ', '; } // the line below gets rid of the comma $sql = substr($sql, 0, strlen($sql) - 2); $sql .= ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'; $ok = $this->createRequest($sql); if (!$ok) { return false; } // ensure it's empty $sql = 'TRUNCATE TABLE ' . $tablename . ';'; $ok = $this->deleteRequest($sql); if ($mismatchtable) { $sql2 = substr($sql2, 0, strlen($sql2) - 2); $sql2 .= ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'; $ok = $this->createRequest($sql2); $sql2 = 'TRUNCATE TABLE ' . $mismatchtable . ';'; $ok = $this->deleteRequest($sql2); } // now insert data $logInstance->setSilentMode(); $entry = array(); if ($mismatchtable) { $entry2 = array(); } foreach ($data as $key => $row) { $sql = 'INSERT INTO ' . $tablename . ' VALUES (\''; foreach ($row as $field => $value) { if ($coltype[$field] == 'float' && !StringTool::isFloat($value) && !StringTool::isInt($value) || $coltype[$field] == 'int(11)' && !StringTool::isInt($value) && !StringTool::isFloat($value)) { if ($mismatchtable) { $entry2[] = $value == "" ? "NULL" : "1:" . $value; // store mismatches directly in this table, with "1:" prefix } $value = "NULL"; } elseif ($value !== "" && !is_null($value)) { if ($mismatchtable && $field != SGBD_SDF_MOLSTRUCCOL) { $entry2[] = 0; } } else { $value = "NULL"; if ($mismatchtable) { $entry2[] = "NULL"; } } $entry[] = $value; } $sql .= implode("','", $entry); $sql .= '\');'; $sql = str_replace('\'NULL\'', 'NULL', $sql); $entry = array(); $ok = $this->insertRequest($sql); if ($mismatchtable) { $sql2 = 'INSERT INTO ' . $mismatchtable . ' VALUES (\''; $sql2 .= implode("','", $entry2); $sql2 .= '\');'; $sql2 = str_replace('\'NULL\'', 'NULL', $sql2); $ok = $this->insertRequest($sql2); $entry2 = array(); } } $logInstance->unsetSilentMode(); return true; } catch (ParameterException $e) { return false; } }
require_once '../../config/constants.php'; $globalsDir = '../../globals'; // Creates folder if needed if (!is_dir($globalsDir)) { if (!mkdir($globalsDir)) { exit('Le dossier n\'a pas pu �tre cr��'); } } if (!is_writable($globalsDir . '/globals.php')) { exit('Le fichier ne peut pas être lu'); } $globalsFile = fopen($globalsDir . '/globals.php', 'w'); // Adds anchors fwrite($globalsFile, "<?php\n"); foreach ($constantList as $constantName => $constantValue) { if ($constantValue === true) { $value = 'true'; } else { if ($constantValue === false) { $value = 'false'; } else { $value = StringTool::isFloat($constantValue) || StringTool::isInt($constantValue) ? $constantValue : '\'' . $constantValue . '\''; } } fwrite($globalsFile, 'define(\'' . $constantName . '\', ' . $value . ");\n"); } // Adds anchors fwrite($globalsFile, '?>'); fclose($globalsFile); echo 'Globals generated<br>'; echo '<a href="../game.php">Back to game</a>';
/** * Returns float value contained in $string * @param string $string The original string * @return float The float value */ public static function toFloat($string) { if (!StringTool::isFloat($string)) { throw new Exception('String to convert is not a float value: ' . $string); } return (double) $string; // faster }
/** * Gets property value in correct format for request * @param string $propertyName The name of the property to detect field type * @param string $propertyValue The property value * @return string the value in correct format for the request */ private static function getFormattedPropertyValue($elementClass, $propertyName, $propertyValue) { // NULL value if ($propertyValue === null) { $formattedValue = 'NULL'; } else { $formattedValue = '\''; if (StringTool::isInt($propertyValue)) { // Datetime / date property type if (StringTool::endsWith($propertyName, 'date')) { $formattedValue .= DateTool::timestampToString($propertyValue); } else { $formattedValue .= StringTool::toInt($propertyValue, FALSE); } } else { if (StringTool::isFloat($propertyValue, FALSE)) { $formattedValue .= StringTool::toFloat($propertyValue, FALSE); } else { $formattedValue .= $elementClass::getDatabaseConnection()->realEscapeString($propertyValue); } } $formattedValue .= '\''; } return $formattedValue; }