Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display randomly-selected albums #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Config/Schema/sonerezh_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function after($event = array()) {
'convert_to' => array('type' => 'string', 'null' => false, 'default' => 'mp3', 'length' => 5, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'quality' => array('type' => 'integer', 'null' => false, 'default' => '256', 'length' => 3, 'unsigned' => true),
'enable_mail_notification' => array('type' => 'boolean', 'null' => false, 'default' => '0'),
'enable_random' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'enable_recent' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'sync_token' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
Expand Down
19 changes: 18 additions & 1 deletion app/Controller/SongsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,17 @@ public function albums() {
));

$latests = array();
$rands = array();
// Is this the first page requested?
$page = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
$db = $this->Song->getDataSource();

if ($db->config['datasource'] == 'Database/Mysql') {
$random_order_by_method = 'RAND()';
} else {
$random_order_by_method = 'RANDOM()';
}

$this->Song->virtualFields['cover'] = 'MIN(Song.cover)';

if ($page == 1) {
Expand All @@ -343,6 +350,12 @@ public function albums() {
'order' => 'MAX(Song.created) DESC',
'limit' => 6
));
$rands = $this->Song->find('all', array(
'fields' => array('Song.id', 'Song.band', 'Song.album', 'Song.cover'),
'group' => 'Song.album',
'order' => $random_order_by_method,
'limit' => 6
));
}

$this->Paginator->settings = array(
Expand All @@ -364,11 +377,15 @@ public function albums() {
$latest['Song']['cover'] = empty($latest['Song']['cover']) ? "no-cover.png" : THUMBNAILS_DIR.'/'.$latest['Song']['cover'];
}

foreach ($rands as &$r) {
$r['Song']['cover'] = empty($r['Song']['cover']) ? "no-cover.png" : THUMBNAILS_DIR.'/'.$r['Song']['cover'];
}

if (empty($songs)) {
$this->Flash->info(__('Oops! The database is empty...'));
}

$this->set(compact('songs', 'playlists', 'latests'));
$this->set(compact('songs', 'playlists', 'latests', 'rands'));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/Locale/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ msgstr ""
msgid "All albums"
msgstr ""

#: View/Songs/albums.ctp:48
msgid "Random albums"
msgstr ""

#: View/Songs/albums.ctp:52
msgid "Sort by album"
msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions app/Locale/fra/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,10 @@ msgstr "Ajoutés récemment"
msgid "All albums"
msgstr "Albums"

#: View/Songs/albums.ctp:48
msgid "Random albums"
msgstr "Au hasard"

#: View/Songs/albums.ctp:52
msgid "Sort by album"
msgstr "Trier par album"
Expand Down
14 changes: 13 additions & 1 deletion app/Model/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class Setting extends AppModel {
'rule' => array('boolean'),
'message' => 'Something went wrong!'
)
),
'enable_random' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => 'Something went wrong!'
)
),
'enable_recent' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => 'Something went wrong!'
)
)
);

Expand Down Expand Up @@ -63,4 +75,4 @@ public function convConflicts($options = array()) {
return true;
}

}
}
12 changes: 11 additions & 1 deletion app/View/Settings/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ $this->end(); ?>
</span>
</small>

<?php
echo $this->Form->input('enable_random', array(
'type' => 'checkbox',
'label' => __('Enable display of random albums.')
));
echo $this->Form->input('enable_recent', array(
'type' => 'checkbox',
'label' => __('Enable display of recently added albums.')
));
?>

<div class="panel <?php echo $avconv ? 'panel-default' : 'panel-danger'; ?>">
<div class="panel-heading">
Expand Down Expand Up @@ -180,4 +190,4 @@ $this->end(); ?>
</p>
</div>
</div>
</div>
</div>
133 changes: 93 additions & 40 deletions app/View/Songs/albums.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,100 @@ echo $this->Html->script('albums');
$this->end(); ?>

<div class="col-lg-12" data-scroll-container="true" data-view="albums">
<?php if (!empty($latests) && $this->Paginator->current('Song') == 1): ?>
<h3><?php echo __('Recently added'); ?></h3>
<?php $i = 1; ?>
<?php $hidden = ''; ?>
<?php foreach ($latests as $album): ?>
<?php if ($i == 4) {
$hidden = 'hidden-xs';
} elseif ($i >= 5) {
$hidden = 'hidden-sm hidden-xs';
} ?>
<div class="col-md-2 col-sm-3 col-xs-4 action-expend <?php echo $hidden; ?>" data-band="<?php echo h($album['Song']['band']); ?>" data-album="<?php echo h($album['Song']['album']); ?>" data-scroll-content="true">
<?php echo $this->Image->lazyload($this->Image->resizedPath($album['Song']['cover'], 220, 220), array('class' => 'img-responsive cover lzld', 'style' => 'cursor: pointer;')); ?>
<h4 class="truncated-name" title="<?php echo h($album['Song']['album']); ?>">
<?php echo h($album['Song']['album']); ?>
<small>
<br /><?php echo h($album['Song']['band']); ?>
</small>
</h4>
</div>
<?php
$clear = false;
if ($i % 6 == 0) {
$clear = "visible-lg visible-md visible-xs";
if ( $i % 4 == 0) {
$clear .= " visible-sm";
}
} elseif ($i % 4 == 0) {
$clear = "visible-sm";
} elseif ($i % 3 == 0) {
$clear = "visible-xs";
}
?>
<?php if ($clear !== false): ?>
<div class="clearfix <?php echo $clear; ?>" data-scroll-content="true"></div>
<?php endif;
$i++; ?>
<?php endforeach; ?>
<div class="clearfix"></div>
<hr />
<?php
$setting = ClassRegistry::init('Setting');
$settings = $setting->find('first', array('fields' => array('Setting.enable_random')));
$random_enabled = $settings['Setting']['enable_random'];
$settings = $setting->find('first', array('fields' => array('Setting.enable_recent')));
$recent_enabled = $settings['Setting']['enable_recent'];
?>

<?php if ($recent_enabled): ?>
<?php if (!empty($latests) && $this->Paginator->current('Song') == 1): ?>
<h3><?php echo __('Recently added'); ?></h3>
<?php $i = 1; ?>
<?php $hidden = ''; ?>
<?php foreach ($latests as $album): ?>
<?php if ($i == 4) {
$hidden = 'hidden-xs';
} elseif ($i >= 5) {
$hidden = 'hidden-sm hidden-xs';
} ?>
<div class="col-md-2 col-sm-3 col-xs-4 action-expend <?php echo $hidden; ?>" data-band="<?php echo h($album['Song']['band']); ?>" data-album="<?php echo h($album['Song']['album']); ?>" data-scroll-content="true">
<?php echo $this->Image->lazyload($this->Image->resizedPath($album['Song']['cover'], 220, 220), array('class' => 'img-responsive cover lzld', 'style' => 'cursor: pointer;')); ?>
<h4 class="truncated-name" title="<?php echo h($album['Song']['album']); ?>">
<?php echo h($album['Song']['album']); ?>
<small>
<br /><?php echo h($album['Song']['band']); ?>
</small>
</h4>
</div>
<?php
$clear = false;
if ($i % 6 == 0) {
$clear = "visible-lg visible-md visible-xs";
if ( $i % 4 == 0) {
$clear .= " visible-sm";
}
} elseif ($i % 4 == 0) {
$clear = "visible-sm";
} elseif ($i % 3 == 0) {
$clear = "visible-xs";
}
?>
<?php if ($clear !== false): ?>
<div class="clearfix <?php echo $clear; ?>" data-scroll-content="true"></div>
<?php endif;
$i++; ?>
<?php endforeach; ?>
<div class="clearfix"></div>
<hr />
<?php endif; ?>
<?php endif; ?>

<?php if ($random_enabled): ?>
<?php if (!empty($rands) && $this->Paginator->current('Song') == 1): ?>
<h3><?php echo __('Random albums'); ?></h3>
<?php $i = 1; ?>
<?php $hidden = ''; ?>
<?php foreach ($rands as $album): ?>
<?php if ($i == 4) {
$hidden = 'hidden-xs';
} elseif ($i >= 5) {
$hidden = 'hidden-sm hidden-xs';
} ?>
<div class="col-md-2 col-sm-3 col-xs-4 action-expend <?php echo $hidden; ?>" data-band="<?php echo h($album['Song']['band']); ?>" data-album="<?php echo h($album['Song']['album']); ?>" data-scroll-content="true">
<?php echo $this->Image->lazyload($this->Image->resizedPath($album['Song']['cover'], 220, 220), array('class' => 'img-responsive cover lzld', 'style' => 'cursor: pointer;')); ?>
<h4 class="truncated-name" title="<?php echo h($album['Song']['album']); ?>">
<?php echo h($album['Song']['album']); ?>
<small>
<br /><?php echo h($album['Song']['band']); ?>
</small>
</h4>
</div>
<?php
$clear = false;
if ($i % 6 == 0) {
$clear = "visible-lg visible-md visible-xs";
if ( $i % 4 == 0) {
$clear .= " visible-sm";
}
} elseif ($i % 4 == 0) {
$clear = "visible-sm";
} elseif ($i % 3 == 0) {
$clear = "visible-xs";
}
?>
<?php if ($clear !== false): ?>
<div class="clearfix <?php echo $clear; ?>" data-scroll-content="true"></div>
<?php endif;
$i++; ?>
<?php endforeach; ?>
<div class="clearfix"></div>
<hr />
<?php endif; ?>
<?php endif; ?>

<?php if (!empty($songs)): ?>
<h3>
<?php echo __('All albums'); ?>
Expand Down Expand Up @@ -93,4 +146,4 @@ $this->end(); ?>
<?php endforeach; ?>
</div>
<?php echo $this->element('add_to_playlist'); ?>
<?php echo $this->element('pagination'); ?>
<?php echo $this->element('pagination'); ?>