datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/helpers.php

143 lines
3.6 KiB

6 years ago
<?php
6 years ago
const VALI_NAME = 'string|regex:/^[a-z0-9_.-]+$/i|max:255';
const VALI_PASSWORD = 'string|min:6|max:1024';
const VALI_EMAIL = 'string|email|max:255';
const VALI_TEXT = 'string|max:4095';
const VALI_LINE = 'string|max:255';
const FEATURE_FORKS = false;
const FEATURE_FAVES = false;
const FEATURE_TABLE_COMMENTS = false;
const FEATURE_PROPOSALS = false;
6 years ago
// global helpers
function authed() {
return ! \Auth::guest();
}
function guest() {
return \Auth::guest();
}
/**
* @return \App\Models\User|null
*/
function user() {
return \Auth::user();
}
function ellipsis($s, $len) {
return \MightyPork\Utils\Str::ellipsis($s, $len);
}
function faker() {
static $fac = null;
if ($fac !== null) return $fac;
return $fac = Faker\Factory::create();
}
6 years ago
function dark_mode($ifDark=true, $ifLight=false) {
return isset($_COOKIE["dark_mode"]) ? $ifDark : $ifLight;
}
6 years ago
/**
* Recursively expand validation rules
*
* @param $arr
* @return array
*/
function vali($arr) {
$result = [];
foreach ($arr as $key => $rules) {
// top level
if (is_array($rules)) {
$ar = [];
foreach ($rules as $rule) {
if (is_string($rule) && strpos($rule, '|') !== false) {
foreach (explode('|', $rule) as $rr) {
$ar[] = $rr;
}
} else if (is_array($rule)) {
// nested array, assume no further recursion
foreach ($rule as $rr) {
$ar[] = $rr;
}
} else {
// Rule
$ar[] = $rule;
}
}
$result[$key] = $ar;
} else {
// string or Rule
$result[$key] = $rules;
}
}
return $result;
}
/**
* like old(), but decodes stringified json
*
* @param string $name
* @param object|array $default
* @return object|array
*/
function old_json($name, $default) {
$old = old($name, null);
if (is_string($old)) return json_decode($old);
return $default;
}
// Safe JSON funcs
function toJSON($object, $emptyObj=false) {
if ($emptyObj) {
if ((empty($object) || ($object instanceof \Illuminate\Support\Collection && $object->count()==0))) {
return '{}';
}
}
if (!$object instanceof JsonSerializable && $object instanceof \Illuminate\Contracts\Support\Arrayable) {
$object = $object->toArray();
}
return \GuzzleHttp\json_encode($object, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
}
function fromJSON($object, $assoc=false) {
return \GuzzleHttp\json_decode($object, $assoc);
}
6 years ago
/**
* @param \Illuminate\Support\Collection|array $items
* @param int $per_page
* @param null|\Closure $mapFn
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
function collection_paginate($items, $per_page, $mapFn = null)
{
if (!$items instanceof \Illuminate\Support\Collection) {
$items = collect($items);
}
$page = Request::get('page', 1);
$pageItems = $items->forPage($page, $per_page)->values();
if (count($pageItems) == 0 && $page > 1) {
$page = 1;
Request::replace(['page' => $page]);
$pageItems = $items->forPage($page, $per_page)->values();
}
6 years ago
return new Illuminate\Pagination\LengthAwarePaginator(
$mapFn ? $pageItems->map($mapFn) : $pageItems,
$items->count(),
$per_page,
Illuminate\Pagination\Paginator::resolveCurrentPage(),
['path' => Illuminate\Pagination\Paginator::resolveCurrentPath()]
);
}