public function view()
 {
     $view = $this->getView();
     $factory = new ModelFactory('GalleryImageModel');
     if ($this->getSetting('order') == 'random') {
         $factory->order('RAND()');
     } else {
         $factory->order($this->getSetting('order'));
     }
     if ($this->getSetting('album')) {
         $factory->where('albumid = ' . $this->getSetting('album'));
         $album = GalleryAlbumModel::Construct($this->getSetting('album'));
         $link = $album->get('baseurl');
     } else {
         $link = null;
     }
     $factory->limit($this->getSetting('count'));
     $images = $factory->get();
     $view->assign('images', $images);
     $view->assign('dimensions', $this->getSetting('dimensions'));
     $view->assign('link', $link);
     $view->assign('uselightbox', $this->getSetting('uselightbox') && Core::IsComponentAvailable('jquery-lightbox'));
 }
 private function _viewAlbum(GalleryAlbumModel $album)
 {
     $view = $this->getView();
     $manager = \Core\user()->checkAccess('p:/gallery/manage_all');
     $editor = \Core\user()->checkAccess($album->get('editpermissions')) || $manager;
     $uploader = \Core\user()->checkAccess($album->get('uploadpermissions')) || $editor;
     $url = $album->get('rewriteurl');
     $images = $album->getLink('GalleryImage', 'weight');
     $lastupdated = $album->get('updated');
     if ($uploader) {
         $uploadform = new Form();
         $uploadform->set('action', \Core\resolve_link('/gallery/images_update/' . $album->get('id')));
         $uploadform->addElement('multifile', array('basedir' => $album->getFullUploadDirectory(), 'title' => 'Bulk Upload Files', 'name' => 'images', 'accept' => $album->get('accepttypes')));
         $uploadform->addElement('submit', array('value' => 'Save Gallery Changes'));
     } else {
         $uploadform = false;
     }
     // I need to attach a friendly URL for each image.
     // This gets a little tricky since each image doesn't have a unique title necessarily.
     foreach ($images as $i) {
         // I would like to know when the last change overall was, not just for the gallery.
         $lastupdated = max($lastupdated, $i->get('updated'));
     }
     $view->templatename = '/pages/gallery/view.tpl';
     $view->assign('album', $album);
     $view->assign('images', $images);
     $view->assign('editor', $editor);
     $view->assign('manager', $manager);
     $view->assign('uploader', $uploader);
     $view->assign('uploadform', $uploadform);
     $view->assign('userid', \Core\user()->get('id'));
     $view->updated = $lastupdated;
     // If there are images in this gallery, grab the first one to show as a preview!
     if (count($images)) {
         $view->meta['og:image'] = $images[0]->getFile()->getPreviewURL('200x200');
     }
     if ($editor) {
         $view->addControl('Gallery Albums Administration', '/gallery/admin', 'directory');
         $view->addControl('Edit Gallery Album', '/gallery/edit/' . $album->get('id'), 'edit');
     }
     if ($uploader) {
         // If they can upload images, they can rearrange them!
         $view->addControl(array('title' => 'Rearrange Images', 'link' => '/gallery/order/' . $album->get('id'), 'icon' => 'move'));
     }
 }
<?php

// Load the necessary files to do this
require_once ROOT_PDIR . 'components/gallery/models/GalleryAlbumModel.php';
$albums = GalleryAlbumModel::Find(null, null, null);
foreach ($albums as $album) {
    /** @var $album GalleryAlbumModel */
    // Set this album's cached title from the page.
    $album->set('title', $album->getLink('Page')->get('title'));
    $album->save();
}
 /**
  * Page for creating and updating a gallery widget
  */
 public function update()
 {
     $view = $this->getView();
     $request = $this->getPageRequest();
     if (!\Core\user()->checkAccess('p:/gallery/manage_all')) {
         return View::ERROR_ACCESSDENIED;
     }
     if ($request->getParameter(0)) {
         $model = new WidgetModel('/gallery/view/' . $request->getParameter(0));
     } else {
         $model = new WidgetModel();
     }
     // The settings and their default values
     $defaults = array('album' => '', 'count' => 5, 'order' => 'weight', 'dimensions' => '100x75', 'uselightbox' => false);
     $settings = array();
     foreach ($defaults as $key => $def) {
         $settings[$key] = $model->getSetting($key) ? $model->getSetting($key) : $def;
     }
     $isnew = !$model->exists();
     $form = new Form();
     $form->set('callsmethod', 'GalleryFormHandler::SaveWidgetHandler');
     $form->addElement('system', array('name' => 'id', 'value' => $request->getParameter(0)));
     $form->addElement('text', array('name' => 'title', 'required' => true, 'title' => 'Widget Title', 'value' => $model->get('title'), 'description' => 'Just the identifying title used on admin pages.'));
     // The options herein are pic the gallery to display from,
     // pick how many images to show,
     // and order to retrieve them.
     $albums = GalleryAlbumModel::Find(null, null, 'title');
     $albumopts = array('' => 'All Galleries');
     foreach ($albums as $album) {
         $albumopts[$album->get('id')] = $album->get('title');
     }
     $form->addElement('select', array('name' => 'album', 'value' => $settings['album'], 'title' => 'Gallery Album', 'options' => $albumopts));
     $form->addElement('select', array('name' => 'count', 'title' => 'Number of thumbnails', 'value' => $settings['count'], 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
     $form->addElement('select', array('name' => 'order', 'title' => 'Order By', 'value' => $settings['order'], 'options' => array('weight' => 'Standard Order', 'created desc' => 'Date (Newest First)', 'random' => 'Random')));
     $form->addElement('text', array('name' => 'dimensions', 'title' => 'Thumbnail Dimensions', 'value' => $settings['dimensions'], 'description' => 'Enter the desired thumbnail dimensions, in the format of (for example), 100x75, width then height separated by an "x" and no spaces.'));
     if (Core::IsComponentAvailable('jquery-lightbox')) {
         $form->addElement('checkbox', array('name' => 'uselightbox', 'checked' => $settings['uselightbox'], 'value' => 1, 'title' => 'Use Lightbox', 'description' => 'Check to open images in a lightbox window for quick previewing.'));
     } else {
         $form->addElement('hidden', array('name' => 'uselightbox', 'value' => 0));
     }
     $form->addElement('submit', array('value' => ($isnew ? 'Create' : 'Update') . ' Widget'));
     $view->templatename = 'pages/gallerywidget/update.tpl';
     $view->mastertemplate = 'admin';
     $view->title = ($isnew ? 'Create' : 'Update') . ' Gallery Widget';
     $view->assign('form', $form);
     $view->addControl('Gallery Widgets', '/gallerywidget/admin', 'directory');
 }