/**
  * 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');
 }
	/**
	 * Delete a simple widget.
	 */
	public function delete(){
		$view = $this->getView();
		$request = $this->getPageRequest();

		if(!\Core\user()->checkAccess('p:/core/widgets/manage')){
			return View::ERROR_ACCESSDENIED;
		}

		if(!$request->isPost()){
			return View::ERROR_BADREQUEST;
		}

		$baseurl = $request->getParameter('baseurl');
		$class = substr($baseurl, 0, strpos($baseurl, '/')) . 'widget';

		if(!class_exists($class)){
			\Core\set_message('Class [' . $class . '] was not found on the system, invalid widget!', 'error');
			\Core\go_back();
		}

		/** @var Widget_2_1 $obj */
		$obj = new $class();

		if(!($obj instanceof Widget_2_1)){
			\Core\set_message('Wrong parent class for [' . $class . '], it does not appear to be a Widget_2_1 instance, invalid widget!', 'error');
			\Core\go_back();
		}

		if(!$obj->is_simple){
			\Core\set_message('Widget [' . $class . '] does not appear to be a simple widget.  Only simple widgets can be created via this page.', 'error');
			\Core\go_back();
		}

		$model = new WidgetModel($baseurl);

		$model->delete();
		\Core\set_message('Deleted widget ' . $model->get('title') . ' successfully!', 'success');
		\Core\go_back();
	}
	/**
	 * Internal function to parse and handle the configs in the component.xml file.
	 * This is used for installations and upgrades.
	 *
	 * @param boolean $install   Set to false to force uninstall/disable mode.
	 * @param int     $verbosity (default 0) 0: standard output, 1: real-time, 2: real-time verbose output.
	 *
	 * @return boolean | int
	 * @throws InstallerException
	 */
	public function _parseWidgets($install = true, $verbosity = 0) {
		$overallChanges  = [];
		$overallAction   = $install ? 'Installing' : 'Uninstalling';
		$overallActioned = $install ? 'Installed' : 'Uninstalled';
		$overallSet      = $install ? 'Set' : 'Remove';

		Core\Utilities\Logger\write_debug($overallAction . ' Widgets for ' . $this->getName());

		if(!$install){
			die('@todo Support uninstalling widgets via _parseWidgets!');
		}

		// I need to get the schema definitions first.
		$node = $this->_xmlloader->getElement('widgets');
		//$prefix = $node->getAttribute('prefix');

		// Now, get every table under this node.
		foreach ($node->getElementsByTagName('widget') as $subnode) {
			$baseurl     = $subnode->getAttribute('baseurl');
			$installable = $subnode->getAttribute('installable');
			$title       = $subnode->getAttribute('title');

			if($verbosity == 2){
				CLI::PrintActionStart($overallAction . ' widget ' . $baseurl . ' ("' . $title . '")');
			}

			// Insert/Update the defaults for an entry in the database.
			$m = new WidgetModel($baseurl);
			$action = ($m->exists()) ? 'Updated' : 'Added';

			if (!$m->get('title')){
				// Only set the title if it was previously unset
				$m->set('title', $title);
			}

			$m->set('installable', $installable);
			$saved = $m->save();

			if ($saved){
				if($verbosity == 2){
					CLI::PrintActionStatus(true);
				}
				$changes[] = $action . ' widget [' . $m->get('baseurl') . ']';

				// Is this a new widget and it's an admin installable one?
				// If so install it to the admin widgetarea!
				if($action == 'Added' && $installable == '/admin'){
					$weight = WidgetInstanceModel::Count(
						[
							'widgetarea' => 'Admin Dashboard',
							'page_baseurl' => '/admin',
						]
					) + 1;

					$wi = new WidgetInstanceModel();
					$wi->setFromArray(
						[
							'baseurl' => $m->get('baseurl'),
							'page_baseurl' => '/admin',
							'widgetarea' => 'Admin Dashboard',
							'weight' => $weight
						]
					);
					$wi->save();

					$overallChanges[] = $overallActioned . ' widget ' . $m->get('baseurl') . ' into the admin dashboard!';
				}
			}
			else{
				if($verbosity == 2){
					CLI::PrintActionStatus('skip');
				}
			}
		}

		return (sizeof($overallChanges) > 0) ? $overallChanges : false;
	}