/** * Generate a unqiue text-identifier for a given table and column from any input string. * * Will generate a text identifier from the string (lowercase, alphanum-only), search for the value in the given table/column, * and keep incrementing a suffixed counter until the value is unique. * * @param \pdyn\database\DbDriverInterface $DB An active database connection. * @param string $input Any input text. * @param string $table The table in which to ensure uniqueness. * @param string $field The column in which to ensure uniqueness. * @param array $restriction_list Array of values the text-identifier cannot match. * @return string The generated, unique text identifier. */ public static function generate_slug(\pdyn\database\DbDriverInterface $DB, $input, $table, $field, array $restriction_list = array()) { $i = 0; while (true) { $slug = \pdyn\datatype\Text::make_slug($input); $slug .= $i != 0 ? '_' . $i : ''; // This is so we don't add the increment on the first loop. if (!empty($restriction_list) && in_array($slug, $restriction_list, true)) { $i++; continue; } $found = $DB->get_record($table, [$field => $slug]); if (empty($found)) { break; } $i++; } return $slug; }
/** * Get a list of active sessions. * * @param \pdyn\database\DbDriverInterface $DB An active database connection. * @return array Array of records of active sessions. */ public static function get_online_users(\pdyn\database\DbDriverInterface &$DB) { return $DB->get_records('online_users', ['sess_invalid' => '0'], ['sess_created' => 'DESC']); }
/** * Get an array of user objects based on multiple user IDs. * * @param \pdyn\database\DbDriverInterface $DB A database connection. * @param array $ids Array of user IDs. * @param int $opts Bitmask of options. * @return array Array of user objects for each passed ID, indexed by user id. */ public static function get_by_ids(\pdyn\database\DbDriverInterface $DB, $ids, $opts = self::NO_OPTS) { if (empty($ids)) { return []; } $filters = []; if ($ids !== 'all' && !in_array('*', $ids, true)) { $filters['id'] = $ids; } if (static::opts_contains($opts, self::INCLUDE_DELETED) === false) { $filters['deleted'] = 0; } $order = ['id' => 'DESC']; $retopts = ['idindexed' => 'id']; $users = $DB->get_records(static::DB_TABLE, $filters, $order, '*', 0, null, $retopts); foreach ($users as $userid => $user) { $userid = (int) $userid; $users[$userid] = static::instance_from_record($DB, $user); } return $users; }
/** * Get an array of objects by object IDs. * * @param \pdyn\database\DbDriverInterface $DB A database connection. * @param array|string $ids An array of object IDs to get, if 'all', will get all albums. * @return array An array of found objects. */ public static function get_by_ids(\pdyn\database\DbDriverInterface $DB, $ids, $additionalfilters = []) { if (empty($ids)) { return []; } $filters = []; if ($ids !== 'all' && !in_array('*', $ids, true)) { $filters[static::DB_ID_FIELDNAME] = $ids; } if (!empty($additionalfilters)) { $filters = array_merge($filters, $additionalfilters); } $order = ['timeupdated' => 'DESC']; $retopts = ['idindexed' => static::DB_ID_FIELDNAME]; $items = $DB->get_records(static::DB_TABLE, $filters, $order, '*', 0, null, $retopts); foreach ($items as $itemid => $item) { $items[$itemid] = static::instance_from_record($DB, $item); } return $items; }