| Total Complexity | 95 |
| Total Lines | 825 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Database |
||
| 15 | { |
||
| 16 | public static $utcDateTimeClass; |
||
| 17 | /** |
||
| 18 | * @var EntityManager |
||
| 19 | */ |
||
| 20 | private static $em; |
||
| 21 | private static $connection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Set the DB manager instance. |
||
| 25 | * |
||
| 26 | * @param EntityManager $em |
||
| 27 | */ |
||
| 28 | public function setManager($em) |
||
| 29 | { |
||
| 30 | self::$em = $em; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Set the DB connection instance. |
||
| 35 | */ |
||
| 36 | public function setConnection(Connection $connection) |
||
| 37 | { |
||
| 38 | self::$connection = $connection; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the DB connection instance. |
||
| 43 | * |
||
| 44 | * @return Connection |
||
| 45 | */ |
||
| 46 | public function getConnection() |
||
| 47 | { |
||
| 48 | return self::$connection; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the DB manager instance. |
||
| 53 | * |
||
| 54 | * @return EntityManager |
||
| 55 | */ |
||
| 56 | public static function getManager() |
||
| 57 | { |
||
| 58 | return self::$em; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns the name of the main database. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function get_main_database() |
||
| 67 | { |
||
| 68 | return self::getManager()->getConnection()->getDatabase(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get main table. |
||
| 73 | * |
||
| 74 | * @param string $table |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public static function get_main_table($table) |
||
| 79 | { |
||
| 80 | return $table; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get course table. |
||
| 85 | * |
||
| 86 | * @param string $table |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public static function get_course_table($table) |
||
| 91 | { |
||
| 92 | return DB_COURSE_PREFIX.$table; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Counts the number of rows in a table. |
||
| 97 | * |
||
| 98 | * @param string $table The table of which the rows should be counted |
||
| 99 | * |
||
| 100 | * @return int the number of rows in the given table |
||
| 101 | * |
||
| 102 | * @deprecated |
||
| 103 | */ |
||
| 104 | public static function count_rows($table) |
||
| 105 | { |
||
| 106 | $obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table")); |
||
| 107 | |||
| 108 | return $obj->n; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the number of affected rows in the last database operation. |
||
| 113 | * |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | public static function affected_rows(Statement $result) |
||
| 117 | { |
||
| 118 | return $result->rowCount(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public static function getUTCDateTimeTypeClass() |
||
| 125 | { |
||
| 126 | return isset(self::$utcDateTimeClass) ? self::$utcDateTimeClass : 'Application\DoctrineExtensions\DBAL\Types\UTCDateTimeType'; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Connect to the database sets the entity manager. |
||
| 131 | * |
||
| 132 | * @param array $params |
||
| 133 | * @param string $sysPath |
||
| 134 | * @param string $entityRootPath |
||
| 135 | * @param bool $returnConnection |
||
| 136 | * @param bool $returnManager |
||
| 137 | * |
||
| 138 | * @throws \Doctrine\ORM\ORMException |
||
| 139 | * |
||
| 140 | * @return |
||
| 141 | */ |
||
| 142 | public function connect( |
||
| 143 | $params = [], |
||
| 144 | $sysPath = '', |
||
| 145 | $entityRootPath = '', |
||
| 146 | $returnConnection = false, |
||
| 147 | $returnManager = false |
||
| 148 | ) { |
||
| 149 | $config = self::getDoctrineConfig($entityRootPath); |
||
| 150 | $config->setAutoGenerateProxyClasses(true); |
||
| 151 | |||
| 152 | $config->setEntityNamespaces( |
||
| 153 | [ |
||
| 154 | 'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity', |
||
| 155 | 'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity', |
||
| 156 | 'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity', |
||
| 157 | 'ChamiloSkillBundle' => 'Chamilo\SkillBundle\Entity', |
||
| 158 | 'ChamiloTicketBundle' => 'Chamilo\TicketBundle\Entity', |
||
| 159 | 'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity', |
||
| 160 | ] |
||
| 161 | ); |
||
| 162 | |||
| 163 | $params['charset'] = 'utf8'; |
||
| 164 | $entityManager = EntityManager::create($params, $config); |
||
| 165 | $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH); |
||
| 166 | |||
| 167 | // Registering Constraints |
||
| 168 | /*AnnotationRegistry::registerAutoloadNamespace( |
||
| 169 | 'Symfony\Component', |
||
| 170 | $sysPath."vendor/" |
||
| 171 | );*/ |
||
| 172 | |||
| 173 | AnnotationRegistry::registerLoader( |
||
| 174 | function ($class) use ($sysPath) { |
||
| 175 | $file = str_replace("\\", DIRECTORY_SEPARATOR, $class).".php"; |
||
| 176 | $file = str_replace('Symfony/Component/Validator', '', $file); |
||
| 177 | $file = str_replace('Symfony\Component\Validator', '', $file); |
||
| 178 | $fileToInclude = $sysPath.'vendor/symfony/validator/'.$file; |
||
| 179 | |||
| 180 | if (file_exists($fileToInclude)) { |
||
| 181 | // file exists makes sure that the loader fails silently |
||
| 182 | require_once $fileToInclude; |
||
| 183 | |||
| 184 | return true; |
||
| 185 | } |
||
| 186 | |||
| 187 | $fileToInclude = $sysPath.'vendor/symfony/validator/Constraints/'.$file; |
||
| 188 | if (file_exists($fileToInclude)) { |
||
| 189 | // file exists makes sure that the loader fails silently |
||
| 190 | require_once $fileToInclude; |
||
| 191 | |||
| 192 | return true; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | ); |
||
| 196 | |||
| 197 | AnnotationRegistry::registerFile( |
||
| 198 | $sysPath."vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntity.php" |
||
| 199 | ); |
||
| 200 | |||
| 201 | // Registering gedmo extensions |
||
| 202 | AnnotationRegistry::registerAutoloadNamespace( |
||
| 203 | 'Gedmo\Mapping\Annotation', |
||
| 204 | $sysPath."vendor/gedmo/doctrine-extensions/lib" |
||
| 205 | ); |
||
| 206 | |||
| 207 | Type::overrideType( |
||
| 208 | Type::DATETIME, |
||
| 209 | self::getUTCDateTimeTypeClass() |
||
| 210 | ); |
||
| 211 | |||
| 212 | $listener = new \Gedmo\Timestampable\TimestampableListener(); |
||
| 213 | $entityManager->getEventManager()->addEventSubscriber($listener); |
||
| 214 | |||
| 215 | $listener = new \Gedmo\Tree\TreeListener(); |
||
| 216 | $entityManager->getEventManager()->addEventSubscriber($listener); |
||
| 217 | |||
| 218 | $listener = new \Gedmo\Sortable\SortableListener(); |
||
| 219 | $entityManager->getEventManager()->addEventSubscriber($listener); |
||
| 220 | $connection = $entityManager->getConnection(); |
||
| 221 | $connection->executeQuery('SET sql_mode = "";'); |
||
| 222 | $connection->executeQuery('SET SESSION sql_mode = ""'); |
||
| 223 | |||
| 224 | if ($returnConnection) { |
||
| 225 | return $connection; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ($returnManager) { |
||
| 229 | return $entityManager; |
||
| 230 | } |
||
| 231 | |||
| 232 | $this->setConnection($connection); |
||
| 233 | $this->setManager($entityManager); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Escape MySQL wildcards _ and % in LIKE search. |
||
| 238 | * |
||
| 239 | * @param string $text The string to escape |
||
| 240 | * |
||
| 241 | * @return string The escaped string |
||
| 242 | */ |
||
| 243 | public static function escape_sql_wildcards($text) |
||
| 244 | { |
||
| 245 | $text = api_preg_replace("/_/", "\_", $text); |
||
| 246 | $text = api_preg_replace("/%/", "\%", $text); |
||
| 247 | |||
| 248 | return $text; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Escapes a string to insert into the database as text. |
||
| 253 | * |
||
| 254 | * @param string $string |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public static function escape_string($string) |
||
| 259 | { |
||
| 260 | $string = self::getManager()->getConnection()->quote($string); |
||
| 261 | // The quote method from PDO also adds quotes around the string, which |
||
| 262 | // is not how the legacy mysql_real_escape_string() was used in |
||
| 263 | // Chamilo, so we need to remove the quotes around. Using trim will |
||
| 264 | // remove more than one quote if they are sequenced, generating |
||
| 265 | // broken queries and SQL injection risks |
||
| 266 | return substr($string, 1, -1); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Gets the array from a SQL result (as returned by Database::query). |
||
| 271 | * |
||
| 272 | * @param string $option Optional: "ASSOC","NUM" or "BOTH" |
||
| 273 | * |
||
| 274 | * @return array|mixed |
||
| 275 | */ |
||
| 276 | public static function fetch_array(Statement $result, $option = 'BOTH') |
||
| 277 | { |
||
| 278 | if ($result === false) { |
||
| 279 | return []; |
||
| 280 | } |
||
| 281 | |||
| 282 | return $result->fetch(self::customOptionToDoctrineOption($option)); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Gets an associative array from a SQL result (as returned by Database::query). |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public static function fetch_assoc(Statement $result) |
||
| 291 | { |
||
| 292 | return $result->fetch(PDO::FETCH_ASSOC); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Gets the next row of the result of the SQL query |
||
| 297 | * (as returned by Database::query) in an object form. |
||
| 298 | * |
||
| 299 | * @return mixed |
||
| 300 | */ |
||
| 301 | public static function fetch_object(Statement $result) |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Gets the array from a SQL result (as returned by Database::query) |
||
| 308 | * help achieving database independence. |
||
| 309 | * |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public static function fetch_row(Statement $result) |
||
| 313 | { |
||
| 314 | if ($result === false) { |
||
| 315 | return []; |
||
| 316 | } |
||
| 317 | |||
| 318 | return $result->fetch(PDO::FETCH_NUM); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Frees all the memory associated with the provided result identifier. |
||
| 323 | * |
||
| 324 | * @return bool|null Returns TRUE on success or FALSE on failure. |
||
| 325 | * Notes: Use this method if you are concerned about how much memory is being used for queries that return large result sets. |
||
| 326 | * Anyway, all associated result memory is automatically freed at the end of the script's execution. |
||
| 327 | */ |
||
| 328 | public static function free_result(Statement $result) |
||
| 329 | { |
||
| 330 | $result->closeCursor(); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Gets the ID of the last item inserted into the database. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public static function insert_id() |
||
| 339 | { |
||
| 340 | return self::getManager()->getConnection()->lastInsertId(); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Wrapper for rowCount(). |
||
| 345 | * |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | public static function num_rows(Statement $result) |
||
| 349 | { |
||
| 350 | if ($result === false) { |
||
| 351 | return 0; |
||
| 352 | } |
||
| 353 | |||
| 354 | return $result->rowCount(); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Acts as the relative *_result() function of most DB drivers and fetches a |
||
| 359 | * specific line and a field. |
||
| 360 | * |
||
| 361 | * @param int $row |
||
| 362 | * @param string $field |
||
| 363 | * |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | public static function result(Statement $resource, $row, $field = '') |
||
| 367 | { |
||
| 368 | if ($resource->rowCount() > 0) { |
||
| 369 | $result = $resource->fetchAll(PDO::FETCH_BOTH); |
||
| 370 | |||
| 371 | return $result[$row][$field]; |
||
| 372 | } |
||
| 373 | |||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Wrapper for executeQuery(). |
||
| 379 | * |
||
| 380 | * @param string $query |
||
| 381 | * |
||
| 382 | * @return Statement |
||
| 383 | */ |
||
| 384 | public static function query($query) |
||
| 385 | { |
||
| 386 | $connection = self::getManager()->getConnection(); |
||
| 387 | $result = null; |
||
| 388 | try { |
||
| 389 | $result = $connection->executeQuery($query); |
||
| 390 | } catch (Exception $e) { |
||
| 391 | self::handleError($e); |
||
| 392 | } |
||
| 393 | |||
| 394 | return $result; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Deal with exceptions from the database extension. |
||
| 399 | * |
||
| 400 | * @param Exception $e |
||
| 401 | */ |
||
| 402 | public static function handleError($e) |
||
| 403 | { |
||
| 404 | $debug = api_get_setting('server_type') == 'test'; |
||
| 405 | if ($debug) { |
||
| 406 | // We use Symfony exception handler for better error information |
||
| 407 | $handler = new ExceptionHandler(); |
||
| 408 | $handler->handle($e); |
||
| 409 | exit; |
||
|
|
|||
| 410 | } else { |
||
| 411 | $msg = $e->getMessage(); |
||
| 412 | if (preg_match('/Serialization failure:/', $msg)) { |
||
| 413 | //do nothing except from logging |
||
| 414 | error_log($msg.' - Reported but otherwise ignored'); |
||
| 415 | } else { |
||
| 416 | error_log($msg); |
||
| 417 | api_not_allowed(false, get_lang('GeneralError')); |
||
| 418 | exit; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Transform an SQL option from Chamilo to PDO syntax. |
||
| 425 | * |
||
| 426 | * @param string $option |
||
| 427 | * |
||
| 428 | * @return int |
||
| 429 | */ |
||
| 430 | public static function customOptionToDoctrineOption($option) |
||
| 431 | { |
||
| 432 | switch ($option) { |
||
| 433 | case 'ASSOC': |
||
| 434 | return PDO::FETCH_ASSOC; |
||
| 435 | break; |
||
| 436 | case 'NUM': |
||
| 437 | return PDO::FETCH_NUM; |
||
| 438 | break; |
||
| 439 | case 'BOTH': |
||
| 440 | default: |
||
| 441 | return PDO::FETCH_BOTH; |
||
| 442 | break; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Stores a query result into an array. |
||
| 448 | * |
||
| 449 | * @author Olivier Brouckaert |
||
| 450 | * |
||
| 451 | * @param Statement $result - the return value of the query |
||
| 452 | * @param string $option BOTH, ASSOC, or NUM |
||
| 453 | * |
||
| 454 | * @return array - the value returned by the query |
||
| 455 | */ |
||
| 456 | public static function store_result(Statement $result, $option = 'BOTH') |
||
| 457 | { |
||
| 458 | return $result->fetchAll(self::customOptionToDoctrineOption($option)); |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Build an insert query. |
||
| 463 | * |
||
| 464 | * @param string $table_name |
||
| 465 | * @param array $attributes |
||
| 466 | * @param bool $show_query |
||
| 467 | * |
||
| 468 | * @return false|int |
||
| 469 | */ |
||
| 470 | public static function insert($table_name, $attributes, $show_query = false) |
||
| 471 | { |
||
| 472 | if (empty($attributes) || empty($table_name)) { |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | |||
| 476 | $params = array_keys($attributes); |
||
| 477 | |||
| 478 | if (!empty($params)) { |
||
| 479 | $sql = 'INSERT INTO '.$table_name.' ('.implode(',', $params).') |
||
| 480 | VALUES (:'.implode(', :', $params).')'; |
||
| 481 | |||
| 482 | $statement = self::getManager()->getConnection()->prepare($sql); |
||
| 483 | $result = $statement->execute($attributes); |
||
| 484 | |||
| 485 | if ($show_query) { |
||
| 486 | var_dump($sql); |
||
| 487 | error_log($sql); |
||
| 488 | } |
||
| 489 | |||
| 490 | if ($result) { |
||
| 491 | return (int) self::getManager()->getConnection()->lastInsertId(); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | return false; |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Build an update query. |
||
| 500 | * |
||
| 501 | * @param string $tableName use Database::get_main_table |
||
| 502 | * @param array $attributes Values to updates |
||
| 503 | * Example: $params['name'] = 'Julio'; $params['lastname'] = 'Montoya'; |
||
| 504 | * @param array $whereConditions where conditions i.e array('id = ?' =>'4') |
||
| 505 | * @param bool $showQuery |
||
| 506 | * |
||
| 507 | * @return bool|int |
||
| 508 | */ |
||
| 509 | public static function update( |
||
| 510 | $tableName, |
||
| 511 | $attributes, |
||
| 512 | $whereConditions = [], |
||
| 513 | $showQuery = false |
||
| 514 | ) { |
||
| 515 | if (!empty($tableName) && !empty($attributes)) { |
||
| 516 | $updateSql = ''; |
||
| 517 | $count = 1; |
||
| 518 | |||
| 519 | foreach ($attributes as $key => $value) { |
||
| 520 | if ($showQuery) { |
||
| 521 | echo $key.': '.$value.PHP_EOL; |
||
| 522 | } |
||
| 523 | $updateSql .= "$key = :$key "; |
||
| 524 | if ($count < count($attributes)) { |
||
| 525 | $updateSql .= ', '; |
||
| 526 | } |
||
| 527 | $count++; |
||
| 528 | } |
||
| 529 | |||
| 530 | if (!empty($updateSql)) { |
||
| 531 | //Parsing and cleaning the where conditions |
||
| 532 | $whereReturn = self::parse_where_conditions($whereConditions); |
||
| 533 | |||
| 534 | $sql = "UPDATE $tableName SET $updateSql $whereReturn "; |
||
| 535 | |||
| 536 | $statement = self::getManager()->getConnection()->prepare($sql); |
||
| 537 | |||
| 538 | $result = $statement->execute($attributes); |
||
| 539 | |||
| 540 | if ($showQuery) { |
||
| 541 | var_dump($sql); |
||
| 542 | var_dump($attributes); |
||
| 543 | var_dump($whereConditions); |
||
| 544 | } |
||
| 545 | |||
| 546 | if ($result) { |
||
| 547 | return $statement->rowCount(); |
||
| 548 | } |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | return false; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Experimental useful database finder. |
||
| 557 | * |
||
| 558 | * @todo lot of stuff to do here |
||
| 559 | * @todo known issues, it doesn't work when using LIKE conditions |
||
| 560 | * |
||
| 561 | * @example array('where'=> array('course_code LIKE "?%"')) |
||
| 562 | * @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins')) |
||
| 563 | * @example array('where'=> array('name = "Julio" AND lastname = "montoya"')) |
||
| 564 | * |
||
| 565 | * @param mixed $columns array (or string if only one column) |
||
| 566 | * @param string $table_name |
||
| 567 | * @param array $conditions |
||
| 568 | * @param string $type_result all|first|count |
||
| 569 | * @param string $option |
||
| 570 | * @param bool $debug |
||
| 571 | * |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | public static function select( |
||
| 575 | $columns, |
||
| 576 | $table_name, |
||
| 577 | $conditions = [], |
||
| 578 | $type_result = 'all', |
||
| 579 | $option = 'ASSOC', |
||
| 580 | $debug = false |
||
| 581 | ) { |
||
| 582 | if ($type_result === 'count') { |
||
| 583 | $conditions['LIMIT'] = null; |
||
| 584 | $conditions['limit'] = null; |
||
| 585 | } |
||
| 586 | |||
| 587 | $conditions = self::parse_conditions($conditions); |
||
| 588 | |||
| 589 | //@todo we could do a describe here to check the columns ... |
||
| 590 | if (is_array($columns)) { |
||
| 591 | $clean_columns = implode(',', $columns); |
||
| 592 | } else { |
||
| 593 | if ($columns === '*') { |
||
| 594 | $clean_columns = '*'; |
||
| 595 | } else { |
||
| 596 | $clean_columns = (string) $columns; |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | if ($type_result === 'count') { |
||
| 601 | $clean_columns = ' count(*) count '; |
||
| 602 | } |
||
| 603 | |||
| 604 | $sql = "SELECT $clean_columns FROM $table_name $conditions"; |
||
| 605 | if ($debug) { |
||
| 606 | var_dump($sql); |
||
| 607 | } |
||
| 608 | $result = self::query($sql); |
||
| 609 | |||
| 610 | if ($type_result === 'count') { |
||
| 611 | $row = self::fetch_array($result, $option); |
||
| 612 | if ($row) { |
||
| 613 | return (int) $row['count']; |
||
| 614 | } |
||
| 615 | |||
| 616 | return 0; |
||
| 617 | } |
||
| 618 | |||
| 619 | $array = []; |
||
| 620 | if ($type_result === 'all') { |
||
| 621 | while ($row = self::fetch_array($result, $option)) { |
||
| 622 | if (isset($row['id'])) { |
||
| 623 | $array[$row['id']] = $row; |
||
| 624 | } else { |
||
| 625 | $array[] = $row; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | } else { |
||
| 629 | $array = self::fetch_array($result, $option); |
||
| 630 | } |
||
| 631 | |||
| 632 | return $array; |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'). |
||
| 637 | * |
||
| 638 | * @todo known issues, it doesn't work when using |
||
| 639 | * LIKE conditions example: array('where'=>array('course_code LIKE "?%"')) |
||
| 640 | * |
||
| 641 | * @param array $conditions |
||
| 642 | * |
||
| 643 | * @return string Partial SQL string to add to longer query |
||
| 644 | */ |
||
| 645 | public static function parse_conditions($conditions) |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @param array $conditions |
||
| 739 | * |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | public static function parse_where_conditions($conditions) |
||
| 743 | { |
||
| 744 | return self::parse_conditions(['where' => $conditions]); |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Build a delete query. |
||
| 749 | * |
||
| 750 | * @param string $table_name |
||
| 751 | * @param array $where_conditions |
||
| 752 | * @param bool $show_query |
||
| 753 | * |
||
| 754 | * @return int |
||
| 755 | */ |
||
| 756 | public static function delete($table_name, $where_conditions, $show_query = false) |
||
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Get Doctrine configuration. |
||
| 772 | * |
||
| 773 | * @param string $path |
||
| 774 | * |
||
| 775 | * @return \Doctrine\ORM\Configuration |
||
| 776 | */ |
||
| 777 | public static function getDoctrineConfig($path) |
||
| 778 | { |
||
| 779 | $isDevMode = true; // Forces doctrine to use ArrayCache instead of apc/xcache/memcache/redis |
||
| 780 | $isSimpleMode = false; // related to annotations @Entity |
||
| 781 | $cache = null; |
||
| 782 | $path = !empty($path) ? $path : api_get_path(SYS_PATH); |
||
| 783 | |||
| 784 | $paths = [ |
||
| 785 | //$path.'src/Chamilo/ClassificationBundle/Entity', |
||
| 786 | //$path.'src/Chamilo/MediaBundle/Entity', |
||
| 787 | //$path.'src/Chamilo/PageBundle/Entity', |
||
| 788 | $path.'src/Chamilo/CoreBundle/Entity', |
||
| 789 | $path.'src/Chamilo/UserBundle/Entity', |
||
| 790 | $path.'src/Chamilo/CourseBundle/Entity', |
||
| 791 | $path.'src/Chamilo/TicketBundle/Entity', |
||
| 792 | $path.'src/Chamilo/SkillBundle/Entity', |
||
| 793 | $path.'src/Chamilo/PluginBundle/Entity', |
||
| 794 | //$path.'vendor/sonata-project/user-bundle/Entity', |
||
| 795 | //$path.'vendor/sonata-project/user-bundle/Model', |
||
| 796 | //$path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity', |
||
| 797 | ]; |
||
| 798 | |||
| 799 | $proxyDir = $path.'app/cache/'; |
||
| 800 | |||
| 801 | $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration( |
||
| 802 | $paths, |
||
| 803 | $isDevMode, |
||
| 804 | $proxyDir, |
||
| 805 | $cache, |
||
| 806 | $isSimpleMode |
||
| 807 | ); |
||
| 808 | |||
| 809 | return $config; |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Check if a given table exists. |
||
| 814 | * |
||
| 815 | * @param string $table |
||
| 816 | * |
||
| 817 | * @return bool |
||
| 818 | */ |
||
| 819 | public static function tableExists($table) |
||
| 820 | { |
||
| 821 | return self::getManager()->getConnection()->getSchemaManager()->tablesExist($table); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * List the columns of a given table. |
||
| 826 | * |
||
| 827 | * @param string $table |
||
| 828 | * |
||
| 829 | * @return \Doctrine\DBAL\Schema\Column[] |
||
| 830 | */ |
||
| 831 | public static function listTableColumns($table) |
||
| 834 | } |
||
| 835 | |||
| 836 | public static function escapeField($field) |
||
| 837 | { |
||
| 838 | return self::escape_string(preg_replace("/[^a-zA-Z0-9_.]/", '', $field)); |
||
| 839 | } |
||
| 840 | } |
||
| 841 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.