Esempio n. 1
0
File: Post.php Progetto: R-J/elefant
	/**
	 * Get a list of archive years, months, and count of posts.
	 */
	public static function archive_months ($published = true) {
		$db = \DB::get_connection (1);
		$dbtype = $db->getAttribute (\PDO::ATTR_DRIVER_NAME);
		$published = $published ? 'where published = "yes"' : '';
		switch ($dbtype) {
			case 'pgsql':
				$res = \DB::fetch (
					'select extract(year from ts) as year, extract(month from ts) as month, count(*) as total
					 from #prefix#blog_post
					 ' . $published . '
					 group by year, month
					 order by year desc, month desc'
				);
				break;
			case 'mysql':
				$res = \DB::fetch (
					'select year(ts) as year, month(ts) as month, count(*) as total
					 from #prefix#blog_post
					 ' . $published . '
					 group by year, month
					 order by year desc, month desc'
				);
				break;
			case 'sqlite':
				$res = \DB::fetch (
					'select strftime(\'%Y\', ts) as year, strftime(\'%m\', ts) as month, count(*) as total
					 from #prefix#blog_post
					 ' . $published . '
					 group by year, month
					 order by year desc, month desc'
				);
				break;
		}
		
		foreach ($res as $k => $row) {
			$res[$k]->month = str_pad ($row->month, 2, '0', STR_PAD_LEFT);
			$res[$k]->date = $row->year . '-' . $res[$k]->month;
		}

		return $res;
	}
Esempio n. 2
0
	/**
	 * Performs a batch of changes wrapped in a database transaction.
	 * The batch `$task` can be an array of items to insert at once,
	 * or a closure function that executes a series of tasks and
	 * performs whatever logic necessary. If any insert fails, or if
	 * the function returns false, the transaction will be rolled
	 * back, otherwise it will be committed. For databases that support
	 * it, records will be inserted using a single SQL insert statement
	 * for greater efficiency.
	 */
	public static function batch ($tasks) {
		DB::execute ('begin');
		if ($tasks instanceof Closure) {
			if ($tasks () === false) {
				self::$batch_error = DB::error ();
				DB::execute ('rollback');
				return false;
			}
		} elseif (is_array ($tasks)) {
			// Check the driver type, because SQLite doesn't support
			// multiple row inserts
			$db = DB::get_connection (1);
			if (! $db) {
				self::$batch_error = 'No database connection';
				return false;
			}

			if ($db->getAttribute (PDO::ATTR_DRIVER_NAME) === 'sqlite') {
				$class = get_called_class ();
				foreach ($tasks as $task) {
					$o = new $class ($task);
					if (! $o->put ()) {
						self::$batch_error = $o->error;
						DB::execute ('rollback');
						return false;
					}
				}
				return DB::execute ('commit');
			}

			// Build the multi-row insert statement
			$class = get_called_class ();
			$o = new $class;
			$sql = 'insert into `' . $o->table . '` (';
			$data = array ();

			// Figure out how many placeholders are needed per record
			$ins = array ();
			$len = count ($tasks[0]);
			for ($i = 0; $i < $len; $i++) {
				$ins[] = '?';
			}

			// Add fields to statement
			$sql .= join (', ', Model::backticks (array_keys ($tasks[0]))) . ') values ';
			$sep = '';

			// Add each record to the statement
			foreach ($tasks as $task) {
				$data = array_merge ($data, array_values ($task));
				$sql .= $sep . '(' . join (', ', $ins) . ')';
				$sep = ', ';
			}

			if (! DB::execute ($sql, $data)) {
				self::$batch_error = DB::error ();
				DB::execute ('rollback');
				return false;
			}
		}
		return DB::execute ('commit');
	}
Esempio n. 3
0
<?php

$page->layout = 'admin';
$this->require_admin();
if ($this->installed('user', $appconf['Admin']['version']) === true) {
    $page->title = i18n_get('Upgrade completed');
    echo '<p><a href="/user/admin">' . i18n_get('Continue') . '</a></p>';
    return;
}
$page->title = i18n_get('Upgrading User App');
$db = DB::get_connection(1);
$dbtype = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
switch ($dbtype) {
    case 'pgsql':
        DB::execute('alter table "user" alter column "password" type varchar(128)');
        break;
    case 'mysql':
        DB::execute('alter table `user` change column `password` `password` varchar(128) not null');
        break;
    case 'sqlite':
        DB::execute('begin transaction');
        DB::execute('alter table `user` rename to `tmp_user`');
        DB::execute('create table user (
			id integer primary key,
			email char(72) unique not null,
			password char(128) not null,
			session_id char(32) unique,
			expires datetime not null,
			name char(72) not null,
			type char(32) not null,
			signed_up datetime not null,
Esempio n. 4
0
 */
$this->require_admin();
if (!isset($_GET['extends'])) {
    echo $this->error(500, __('Unknown error'));
    return;
}
if (!class_exists($_GET['extends'])) {
    echo $this->error(500, __('Unknown error'));
    return;
}
if (!isset($_GET['name'])) {
    $_GET['name'] = $_GET['extends'];
}
// Create the database table if it doesn't exist
if (!DB::single('select count(*) from #prefix#extended_fields')) {
    $db = DB::get_connection(true);
    $queries = sql_split(file_get_contents(sprintf('apps/admin/conf/update/extended_fields_%s.sql', $db->getAttribute(PDO::ATTR_DRIVER_NAME))));
    foreach ($queries as $query) {
        DB::execute($query);
    }
}
$page->layout = 'admin';
$page->title = __('Custom Fields') . ': ' . __($_GET['name']);
$page->add_script('/apps/admin/js/handlebars-1.0.rc.1.js');
$page->add_script('/js/jquery-ui/jquery-ui.min.js');
$page->add_script('/apps/admin/js/extended.js');
$data = array('extends' => $_GET['extends']);
$data['fields'] = ExtendedFields::for_class($_GET['extends']);
if (!is_array($data['fields'])) {
    $data['fields'] = array();
}