/** * Controller view to update any instance-specific options for a given template. * * Usually consists of just access permissions and display template, but more options could come in the future. */ public function instance_movedown(){ $view = $this->getView(); $request = $this->getPageRequest(); if(!\Core\user()->checkAccess('p:/core/widgets/manage')){ return View::ERROR_ACCESSDENIED; } $instance = WidgetInstanceModel::Construct($request->getParameter(0)); if(!$instance->exists()){ return View::ERROR_NOTFOUND; } if(!$request->isPost()){ return View::ERROR_BADREQUEST; } // Figure out which instance is this one -1. $otherCriteria = [ 'site = ' . $instance->get('site'), 'template = ' . ($instance->get('template') === null ? 'NULL' : $instance->get('template')), 'page_baseurl = ' . ($instance->get('page_baseurl') === null ? 'NULL' : $instance->get('page_baseurl')), 'widgetarea = ' . $instance->get('widgetarea'), 'weight = ' . ($instance->get('weight') + 1), ]; $other = WidgetInstanceModel::Find($otherCriteria, 1); if(!$other){ \Core\set_message('Widget is already in the bottom position!', 'error'); } else{ $other->set('weight', $other->get('weight') - 1); $instance->set('weight', $instance->get('weight') + 1); $other->save(); $instance->save(); } \Core\go_back(); }
public function widgetinstances_save(){ $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; } $counter = 0; $changes = ['created' => 0, 'updated' => 0, 'deleted' => 0]; $selected = $_POST['selected']; // For the incoming options, I want an explicit NULL if it's empty. $theme = $_POST['theme'] == '' ? null : $_POST['theme']; $skin = $_POST['skin'] == '' ? null : $_POST['skin']; $_POST['skin']; $template = $_POST['template'] == '' ? null : $_POST['template']; $baseurl = $_POST['page_baseurl'] == '' ? null : $_POST['page_baseurl']; foreach($_POST['widgetarea'] as $id => $dat){ // Merge in the global information for this request //$dat['theme'] = $theme; //$dat['skin'] = $skin; $dat['template'] = $template; $dat['page_baseurl'] = $baseurl; $dat['weight'] = ++$counter; $dat['access'] = $dat['widgetaccess']; $w = WidgetModel::Construct($dat['baseurl']); $dat['site'] = $w->get('site'); if(strpos($id, 'new') !== false){ $w = new WidgetInstanceModel(); $w->setFromArray($dat); $w->save(); $changes['created']++; } elseif(strpos($id, 'del-') !== false){ $w = new WidgetInstanceModel(substr($id, 4)); $w->delete(); // Reset the counter back down one notch since this was a deletion request. --$counter; $changes['deleted']++; } else{ $w = new WidgetInstanceModel($id); $w->setFromArray($dat); if($w->save()) $changes['updated']++; } } // foreach($_POST['widgetarea'] as $id => $dat) // Display some human friendly status message. if($changes['created'] || $changes['updated'] || $changes['deleted']){ $changetext = []; if($changes['created'] == 1) $changetext[] = 'One widget added'; elseif($changes['created'] > 1) $changetext[] = $changes['created'] . ' widgets added'; if($changes['updated'] == 1) $changetext[] = 'One widget updated'; elseif($changes['updated'] > 1) $changetext[] = $changes['updated'] . ' widgets updated'; if($changes['deleted'] == 1) $changetext[] = 'One widget deleted'; elseif($changes['deleted'] > 1) $changetext[] = $changes['deleted'] . ' widgets deleted'; \Core\set_message(implode('<br/>', $changetext), 'success'); } else{ \Core\set_message('t:MESSAGE_INFO_NO_CHANGES_PERFORMED'); } if($baseurl){ \Core\redirect($baseurl); } else{ \Core\redirect('/admin/widgets?selected=' . $selected); } }
/** * 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; }