<commit_before><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;

class Album extends Model
{

	protected $dates = [
		'created_at',
		'updated_at',
		'min_takestamp',
		'max_takestamp'
	];



	public function photos()
	{
		return $this->hasMany('App\Photo', 'album_id', 'id');
	}



	/**
	 * Rurns album-attributes into a front-end friendly format. Note that some attributes remain unchanged.
	 * @return array Returns album-attributes in a normalized structure.
	 */
	public function prepareData()
	{

		// This function requires the following album-attributes and turns them
		// into a front-end friendly format: id, title, public, sysstamp, password
		// Note that some attributes remain unchanged

		// Init
		$album = array();

		// Set unchanged attributes
		$album['id'] = $this->id;
		$album['title'] = $this->title;
		$album['public'] = strval($this->public);
		$album['hidden'] = strval($this->visible_hidden);
		$album['parent_id'] = $this->parent_id;

		// Additional attributes
		// Only part of $album when available
		$album['description'] = strval($this->description);
		$album['visible'] = strval($this->visible_hidden);
		$album['downloadable'] = strval($this->downloadable);

		// Parse date
		$album['sysdate'] = $this->created_at->format('F Y');
		$album['min_takestamp'] = $this->min_takestamp == null ? '' : $this->min_takestamp->format('M Y');
		$album['max_takestamp'] = $this->max_takestamp == null ? '' : $this->max_takestamp->format('M Y');

		// Parse password
		$album['password'] = ($this->password == '' ? '0' : '1');

		$album['license'] = $this->license == 'none' ? Configs::get_value('default_license') : $this->license;

		$album['owner'] = $this->owner->username;

		$album['thumbs'] = array();
		$album['thumbs2x'] = array();
		$album['types'] = array();

		return $album;
	}



	public function gen_thumbs($return)
	{

		$alb = $this->get_all_subalbums();
		$alb[] = $this->id;

		$thumbs_types = Photo::select('thumbUrl', 'thumb2x', 'type')
			->whereIn('album_id', $alb)
			->orderBy('star', 'DESC')
			->orderBy(Configs::get_value('sortingPhotos_col'), Configs::get_value('sortingPhotos_order'))
			->limit(3)->get();

		// For each thumb
		$k = 0;
		foreach ($thumbs_types as $thumb_types) {
			$return['thumbs'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->thumbUrl;
			if ($thumb_types->thumb2x == '1') {
				$thumbUrl2x = explode(".", $thumb_types->thumbUrl);
				$thumbUrl2x = $thumbUrl2x[0].'@2x.'.$thumbUrl2x[1];
				$return['thumbs2x'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumbUrl2x;
			}
			else {
				$return['thumbs2x'][$k] = '';
			}
			$return['types'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->type;
			$k++;
		}

		return $return;
	}


	/**
	 * Recursively returns the tree structure of albums. Private user albums are returned
	 * only if `$userId` is set.
	 *
	 * @param  int   $userId
	 * @return array
	 */
	public function get_albums(int $userId = null) : array
	{
		$subAlbums = [];
		foreach ($this->children as $subAlbum) {

			if (($subAlbum->public == '1' && $subAlbum->visible_hidden == '1') || $userId === 0 || ($userId === $subAlbum->owner->id)) {

				$album = $subAlbum->prepareData();
				$album['albums'] = $subAlbum->get_albums($userId);
				$album = $subAlbum->gen_thumbs($album);

				$subAlbums[] = $album;
			}
		}

		return $subAlbums;
	}



	public function get_all_subalbums($return = array())<commit_msg>Don't return thumbs of password-protected subalbums (#142)<commit_after><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

class Album extends Model
{

	protected $dates = [
		'created_at',
		'updated_at',
		'min_takestamp',
		'max_takestamp'
	];



	public function photos()
	{
		return $this->hasMany('App\Photo', 'album_id', 'id');
	}



	/**
	 * Rurns album-attributes into a front-end friendly format. Note that some attributes remain unchanged.
	 * @return array Returns album-attributes in a normalized structure.
	 */
	public function prepareData()
	{

		// This function requires the following album-attributes and turns them
		// into a front-end friendly format: id, title, public, sysstamp, password
		// Note that some attributes remain unchanged

		// Init
		$album = array();

		// Set unchanged attributes
		$album['id'] = $this->id;
		$album['title'] = $this->title;
		$album['public'] = strval($this->public);
		$album['hidden'] = strval($this->visible_hidden);
		$album['parent_id'] = $this->parent_id;

		// Additional attributes
		// Only part of $album when available
		$album['description'] = strval($this->description);
		$album['visible'] = strval($this->visible_hidden);
		$album['downloadable'] = strval($this->downloadable);

		// Parse date
		$album['sysdate'] = $this->created_at->format('F Y');
		$album['min_takestamp'] = $this->min_takestamp == null ? '' : $this->min_takestamp->format('M Y');
		$album['max_takestamp'] = $this->max_takestamp == null ? '' : $this->max_takestamp->format('M Y');

		// Parse password
		$album['password'] = ($this->password == '' ? '0' : '1');

		$album['license'] = $this->license == 'none' ? Configs::get_value('default_license') : $this->license;

		$album['owner'] = $this->owner->username;

		$album['thumbs'] = array();
		$album['thumbs2x'] = array();
		$album['types'] = array();

		return $album;
	}



	public function gen_thumbs($return)
	{

		$alb = $this->get_all_subalbums();
		$alb[] = $this->id;

		$thumbs_types = Photo::select('thumbUrl', 'thumb2x', 'type')
			->whereIn('album_id', $alb)
			->orderBy('star', 'DESC')
			->orderBy(Configs::get_value('sortingPhotos_col'), Configs::get_value('sortingPhotos_order'))
			->limit(3)->get();

		// For each thumb
		$k = 0;
		foreach ($thumbs_types as $thumb_types) {
			$return['thumbs'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->thumbUrl;
			if ($thumb_types->thumb2x == '1') {
				$thumbUrl2x = explode(".", $thumb_types->thumbUrl);
				$thumbUrl2x = $thumbUrl2x[0].'@2x.'.$thumbUrl2x[1];
				$return['thumbs2x'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumbUrl2x;
			}
			else {
				$return['thumbs2x'][$k] = '';
			}
			$return['types'][$k] = Config::get('defines.urls.LYCHEE_URL_UPLOADS_THUMB').$thumb_types->type;
			$k++;
		}

		return $return;
	}


	/**
	 * Recursively returns the tree structure of albums. Private user albums are returned
	 * only if `$userId` is set.
	 *
	 * @param  int   $userId
	 * @return array
	 */
	public function get_albums(int $userId = null) : array
	{
		$subAlbums = [];
		foreach ($this->children as $subAlbum) {

			if (($subAlbum->public == '1' && $subAlbum->visible_hidden == '1') || $userId === 0 || ($userId === $subAlbum->owner->id)) {

				$album = $subAlbum->prepareData();
				$album['albums'] = $subAlbum->get_albums($userId);
				if ($subAlbum->password === null || Session::get('login')) {
					$album = $subAlbum->gen_thumbs($album);
				}

				$subAlbums[] = $album;
			}
		}

		return $subAlbums;
	}



	public function get_all_subalbums($return = array())
{'fraction_non_alphanumeric': 0.1908229152356093, 'fraction_numerical': 0.006182167880203325, 'mean_word_length': 4.241180705543557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1719969', 'n_tokens_mistral': 2827, 'n_tokens_neox': 2536, 'n_words': 497}