<commit_before><?php namespace Bkwld\Decoy\Models;

// Deps
use Bkwld\Upchuck\SupportsUploads;
use Bkwld\Library\Utils\String;
use Config;
use Decoy;
use DecoyURL;
use HTML;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Hash;
use Input;
use Mail;
use Request;
use URL;

class Admin extends Base implements UserInterface, RemindableInterface {
	use UserTrait, RemindableTrait, SupportsUploads;

	/**
	 * The table associated with the model.  Explicitly declaring so that sub
	 * classes can use it
	 *
	 * @var string
	 */
	protected $table = 'admins';

	/**
	 * Uploadable attributes
	 * 
	 * @var array
	 */
	protected $upload_attributes = ['image'];

	/**
	 * Don't allow cloning because duplicate emails are not allowed.
	 *
	 * @var boolean 
	 */
	public $cloneable = false;

	/**
	 * Admins should not be localized
	 *
	 * @var boolean
	 */
	static public $localizable = false;

	/**
	 * Validation rules
	 * 
	 * @var array
	 */
	public static $rules = [
		'first_name' => 'required',
		'last_name' => 'required',
		'image' => 'image',
		'email' => 'required|email|unique:admins,email',
		'password' => '<PASSWORD>',
		'confirm_password' => '<PASSWORD>',
	];

	/**
	 * Orders instances of this model in the admin
	 * 
	 * @param  Illuminate\Database\Query\Builder $query
	 * @return void
	 */
	public function scopeOrdered($query) {
		$query->orderBy('last_name')->orderBy('first_name');
	}

	/**
	 * Tweak some validation rules
	 *
	 * @param Illuminate\Validation\Validator $validation
	 */
	public function onValidating($validation) {

		// Only apply mods when editing an existing record
		if (!$this->exists) return;
		$rules = self::$rules;

		// Make password optional
		$rules = array_except($rules, 'password');

		// Ignore the current record when validating email
		$rules['email'] .= ','.$this->id;
		
		// Update rules
		$validation->setRules($rules);
	}

	/**
	 * New admin callbacks
	 *
	 * @return void
	 */
	public function onCreating() {

		// Send out email
		if (Input::has('_send_email')) $this->sendCreateEmail();

		// Make them active
		$this->active = 1;

		// If the current user can't grant permissions, make the new admin
		// have the same role as themselves
		if (!app('decoy.auth')->can('grant', 'admins')) {
			$this->role = app('decoy.auth')->user()->role;
		}
	}

	/**
	 * Admin updating callbacks
	 *
	 * @return void 
	 */
	public function onUpdating() {
		if (Input::has('_send_email')) $this->sendUpdateEmail();
	}

	/**
	 * Callbacks regardless of new or old
	 *
	 * @return void <commit_msg>Fix creating admins from CLI, like during migration<commit_after><?php namespace Bkwld\Decoy\Models;

// Deps
use Bkwld\Upchuck\SupportsUploads;
use Bkwld\Library\Utils\String;
use Config;
use Decoy;
use DecoyURL;
use HTML;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Hash;
use Input;
use Mail;
use Request;
use URL;

class Admin extends Base implements UserInterface, RemindableInterface {
	use UserTrait, RemindableTrait, SupportsUploads;

	/**
	 * The table associated with the model.  Explicitly declaring so that sub
	 * classes can use it
	 *
	 * @var string
	 */
	protected $table = 'admins';

	/**
	 * Uploadable attributes
	 * 
	 * @var array
	 */
	protected $upload_attributes = ['image'];

	/**
	 * Don't allow cloning because duplicate emails are not allowed.
	 *
	 * @var boolean 
	 */
	public $cloneable = false;

	/**
	 * Admins should not be localized
	 *
	 * @var boolean
	 */
	static public $localizable = false;

	/**
	 * Validation rules
	 * 
	 * @var array
	 */
	public static $rules = [
		'first_name' => 'required',
		'last_name' => 'required',
		'image' => 'image',
		'email' => 'required|email|unique:admins,email',
		'password' => '<PASSWORD>',
		'confirm_password' => '<PASSWORD>',
	];

	/**
	 * Orders instances of this model in the admin
	 * 
	 * @param  Illuminate\Database\Query\Builder $query
	 * @return void
	 */
	public function scopeOrdered($query) {
		$query->orderBy('last_name')->orderBy('first_name');
	}

	/**
	 * Tweak some validation rules
	 *
	 * @param Illuminate\Validation\Validator $validation
	 */
	public function onValidating($validation) {

		// Only apply mods when editing an existing record
		if (!$this->exists) return;
		$rules = self::$rules;

		// Make password optional
		$rules = array_except($rules, 'password');

		// Ignore the current record when validating email
		$rules['email'] .= ','.$this->id;
		
		// Update rules
		$validation->setRules($rules);
	}

	/**
	 * New admin callbacks
	 *
	 * @return void
	 */
	public function onCreating() {

		// Send out email
		if (Input::has('_send_email')) $this->sendCreateEmail();

		// Make them active
		$this->active = 1;

		// If the current user can't grant permissions, make the new admin
		// have the same role as themselves.  Admins created from the CLI (like as
		// part of a migration) won't be logged in.
		if (($admin = app('decoy.auth')->user())
			&& !app('decoy.auth')->can('grant', 'admins')) {
			$this->role = $admin->role;
		}
	}

	/**
	 * Admin updating callbacks
	 *
	 * @return void 
	 */
	public function onUpdating() {
		if (Input::has('_send_email')) $this->sendUpdateEmail();
	}

	/**
	 * Callbacks regardless of new or old
	 *
	 * @return void 
{'fraction_non_alphanumeric': 0.14217749313815187, 'fraction_numerical': 0.00036596523330283625, 'mean_word_length': 3.5024711696869852, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5194908', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1710, 'n_words': 492}