/** * Change default DB * @param string $newDB */ public static function setDefaultDB($newDB) { $config = self::getInstance(); $config['db'] = $config[$newDB]; $reset = true; $db = new DB(); $db->getConnection($reset); }
/** * Get string from label. * * @param string $_label */ public function getFromLabel($_label) { $db = new DB(); $return = null; $item = $db->query("SELECT id FROM {$this->table} WHERE label LIKE '{$_label}' ORDER BY language"); $result = $item->fetchAll(); if ($result) { foreach ($result as $item) { $l = new self(); $return[] = $l->getFromId($item['id']); } } return $return; }
/** * getList function. */ public function getList() { $db = new DB(); $_table = $this->table; $return = null; if (empty($_table)) { $_table = 'mailing'; } $stm = $db->query("SELECT id FROM {$_table} WHERE language = 1"); if ($stm->errorCode() == 0) { return new Iterators\NoRewindIterator($stm, function ($index, $item) { $mail = new Mail(); $mail->getFromId($item['id']); return [$index, $mail]; }); } return false; }
/** * getFromLabel function. * * @access public * @param mixed $_label * @return void */ public function getFromLabel($_label) { try { $db = new DB(); $_id = (int) $_id; $_table = $this->table; $return = NULL; $item = $db->query("SELECT id FROM {$_table} WHERE label like '{$_label}' ORDER BY language"); $result = $item->fetchAll(); if ($result) { foreach ($result as $item) { $l = new LangStrings(); $return[] = $l->getFromId($item['id']); } } return $return; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die; } }
/** * @param bool $_all * @param bool $_byPriority * @return array */ public function getList($_all = false, $_byPriority = false) { $db = new DB(); $return = array(); $result = null; $_table = $this->table; $where = ''; $order = ''; if ($_byPriority) { $order = 'ORDER BY priority ASC'; } if ($_all) { $where = "WHERE active = 1"; } $items = $db->query("SELECT id FROM {$_table} {$where} {$order}"); if (@$items) { $result = $items->fetchAll(); foreach ($result as $item) { $ob_lang = new Lang(); $return[] = $ob_lang->getFromId($item['id']); } } return @$return; }
public function getFromProject($_id, $_num = 20) { $return = null; try { $db = new DB(); $items = $db->query("SELECT id\n FROM " . $this->table . "\n WHERE project = '{$_id}'\n AND replyTo = '0'\n ORDER BY date DESC LIMIT {$_num}"); $result = $items->fetchAll(); if ($result) { $return = array(); foreach ($result as $item) { $obC = new ProjectComment(); $return[] = $obC->getFromId($item['id']); } } } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die; } return $return; }
/** * typeFormat. * * @param string $_label * @param string $_utf8 * * @return string */ public function typeFormat($_label, $_utf8 = '') { $return = $_label; if (isset($this->id)) { if (isset($this->strings[$_label]) && !empty($this->strings[$_label])) { $return = $this->strings[$_label]; switch ($_utf8) { case 'd': $return = utf8_decode($return); break; case 'e': $return = utf8_encode($return); break; } } else { $db = new DB(); $_table = $this->table_strings; $items = $db->query("SELECT language FROM {$_table} WHERE label = ?", $_label); $label_languages = $items->fetchAll(\PDO::FETCH_COLUMN); if ($label_languages === false) { $label_languages = array(); } $return = "#{$_label}#"; foreach ($this->getList() as $lang) { if (!in_array($lang->id, $label_languages)) { $newString = new \Quaver\App\Model\LangStrings(); $newString->language = $lang->id; $newString->label = $_label; $newString->text = $return; $newString->save(); } } $this->strings[$_label] = $return; } } return $return; }
/** * Internal method for delete the persistent data. * * If you need to do some action overwrite the delete method instead of this. * * @return bool */ public function doDelete() { if (!$this->exists() || $this->isDeleted()) { return true; } if ($this->dispatch('preDelete') === false) { return false; } if (!$this->exists() || $this->isDeleted()) { return true; } try { $db = new DB(); $id = (int) $this->id; $sql = "DELETE FROM {$this->table} WHERE `{$this->primaryKey}` = :id"; if ($db->query($sql, array(':id' => $id))) { $this->isDeleted = true; return true; } else { return false; } } catch (PDOException $e) { throw new QException($e->getMessage()); } $this->dispatch('postDelete'); }
/** * Delete object. */ public function delete() { if (!$this->id || in_array($this->status, ['sent', 'sending', 'deleted'])) { throw new \LogicException('Mail can not be deleted'); } $deleted = date('Y-m-d H:i:s'); $db = new DB(); $sql = "UPDATE {$this->table} SET status = 'deleted', deleted = ? WHERE id = ? AND status NOT IN ('sent', 'sending')"; $result = $db->query($sql, $deleted, $this->id); if (!$result->rowCount()) { throw new \LogicException('Mail can not be delete'); } $this->status = 'deleted'; $this->deleted = $deleted; return $this->save(); }
public function getPoints() { try { $db = new DB(); $_id = $this->id; $item = $db->query("SELECT DISTINCT count(id) as count\n FROM " . $this->table_points . "\n WHERE project = '{$_id}'"); $result = $item->fetchColumn(0); if ($result) { $this->points = $result; return true; } else { $this->points = 0; return false; } } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die; } }
/** * @return bool */ public function delete() { try { $db = new DB(); $_id = (int) $this->id; $_table = $this->table; $sql = "DELETE FROM {$_table} WHERE id = :id"; if ($db->query($sql, array(':id' => $_id))) { return true; } else { return false; } } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die; } }
/** * Check unique constraint of a database column. * * @param mixed $value The value to check * @param string $field The column where check the contraint * @param \Quaver\Model\Base $model The instance of the model where the value will be saved * * @return bool * * @throws \Quaver\Core\QException */ public function isUnique($value, $field, $model) { if ($this->isEmpty($value, $field, $model)) { return true; } $value = $this->cast($value, $field, $model); if ($model->exists() && $this->compareValue($value, $field, $model, true) === 0) { return true; } $db = new DB(); $item = $db->query("SELECT `{$model->primaryKey}`\n FROM {$model->table}\n WHERE `{$field}` = ?", $value); $result = $item->fetchColumn(0); if ($result === false || $result == $model->id) { return true; } return false; }
/** * Delete object. */ public function delete() { try { $db = new DB(); $_id = (int) $this->id; $sql = "DELETE FROM {$this->table} WHERE id = :id"; if ($db->query($sql, array(':id' => $_id))) { return true; } else { return false; } } catch (PDOException $e) { throw new QException($e->getMessage()); } }
/** * getFromEmail. * * @param string $email * * @return bool */ public function getFromEmail($email) { if (!empty($email)) { $db = new DB(); $item = $db->query("SELECT * FROM {$this->table} WHERE email = ?", $email); $result = $item->fetchAll(); if ($result) { $this->setItem($result[0]); return $this; } } return false; }
/** * @return bool */ public function save() { try { $db = new DB(); $set = ''; $values = array(); foreach ($this->_fields as $field) { if ($set != '') { $set .= ', '; } $set .= "{$field} = :{$field}"; $values[":{$field}"] = $this->{$field}; } if (empty($this->id)) { $sql = "INSERT INTO " . $this->table . " SET " . $set; } else { $values[':id'] = $this->id; $sql = "UPDATE " . $this->table . " SET " . $set . " WHERE id = :id"; } $db->query($sql, $values); if (empty($this->id)) { $this->id = $db->insertId(); } return true; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die; } }
/** * Internal method for respond to a getList call. * * If you need to do some action overwrite the getList method instead of this. * * @param string $query (optional) The query to use for get the list * @param array $params (optional) The values to replace in the query and countQuery placeholders * @param array $options (optional) * * @return array Return the current data, the recordsTotal and the total of recordsFiltered */ public function doGetList($query, $params, $options) { $countQuery = empty($options['countQuery']) ? null : $options['countQuery']; $tableAlias = empty($options['tableAlias']) ? $this->table : $options['tableAlias']; $db = new DB(); $isDefaultQuery = empty($query); $isDefaultCountQuery = $isDefaultQuery && empty($countQuery); if (is_null($params)) { $params = []; } $return = ['object' => 'list', 'data' => false, 'recordsTotal' => 0, 'recordsFiltered' => 0, 'error' => false]; // Prepare queries without filters if ($isDefaultQuery) { $query = "SELECT * FROM {$this->table} {$tableAlias}"; } if (empty($countQuery)) { $countQuery = preg_replace('/^\\s*SELECT\\s+.+?\\s+FROM/i', 'SELECT count("") FROM', $query, 1); } // Count the total of rows $item = $db->query($countQuery, $params); $return['recordsTotal'] = $item->fetchColumn(0); $return['recordsFiltered'] = $return['recordsTotal']; // Filter by the search value if (!empty($options['search']['value']) && !empty($options['search']['fields'])) { // Add filters foreach ($options['search']['fields'] as $field) { $sqlFilter = $this->getSqlFieldFilter($tableAlias, $field, $options['search']['value']); if ($sqlFilter) { $sqlSearch[] = $sqlFilter; } } // Edit the sql to add the filters if (!empty($sqlSearch)) { $queryWhere = ''; $countQueryWhere = ''; $extractNestedParenthesisRegex = '([^()]* (?: \\( (?-1)*+ \\) [^()]* )*)'; $extractWhereRegex = "/(.*)\\s+WHERE\\s+{$extractNestedParenthesisRegex}\$/ix"; if (!$isDefaultQuery) { preg_match($extractWhereRegex, $query, $matches); if (!empty($matches)) { $query = $matches[1]; $queryWhere = $matches[2]; } } if (!$isDefaultCountQuery) { preg_match($extractWhereRegex, $countQuery, $matches); if (!empty($matches)) { $countQuery = $matches[1]; $countQueryWhere = $matches[2]; } } $sqlSearch = implode(' OR ', $sqlSearch); $query .= $queryWhere ? " WHERE ({$queryWhere}) AND ({$sqlSearch})" : " WHERE {$sqlSearch}"; $countQuery .= $countQueryWhere ? " WHERE ({$countQueryWhere}) AND ({$sqlSearch})" : " WHERE {$sqlSearch}"; $valueSlashed = addcslashes($options['search']['value'], '%_'); if (strpos($sqlSearch, ':_value_') !== false) { $params[':_value_'] = '%' . $valueSlashed . '%'; } if (strpos($sqlSearch, ':value_') !== false) { $params[':value_'] = '%' . $valueSlashed . '%'; } if (preg_match('/:_value[^_]/', $sqlSearch)) { $params[':_value'] = '%' . $valueSlashed . '%'; } if (preg_match('/:value[^_]/', $sqlSearch)) { $params[':value'] = $options['search']['value']; } // Set the total of records after apply the filter $item = $db->query($countQuery, $params); $return['recordsFiltered'] = $item->fetchColumn(0); } } // Add ORDER BY query section if (!empty($options['order'])) { $queryOrder = []; foreach ($options['order'] as $field => $dir) { if (in_array($dir, ['asc', 'desc'])) { $sqlOrder = $this->getSqlFieldOrder($tableAlias, $field, $dir); if ($sqlOrder) { $queryOrder[] = $sqlOrder; } } } if (!empty($queryOrder)) { $query .= ' ORDER BY ' . implode(',', $queryOrder); } } // get the data if (isset($options['limit'])) { $limit = max(1, $options['limit']); $page = isset($options['page']) ? max(1, $options['page']) : 1; $start = ($page - 1) * $limit; $query .= " LIMIT {$start}, {$limit}"; } $stm = $db->query($query, $params); if ($stm->errorCode() == 0) { $modelName = get_class(); $indexBy = isset($options['indexBy']) ? $options['indexBy'] : false; $return['data'] = new NoRewindIterator($stm, function ($index, $item) use($modelName, $indexBy) { $model = new $modelName(); $model->setItem($item); return [$indexBy ? $model->{$indexBy} : $index, $model]; }); } else { $error = $stm->errorInfo(); $return['error'] = $error[2]; $return['errorCode'] = $error[0]; $return['errorIdentifier'] = $error[1]; } return $return; }