0251 / 590 837 15
info@a-coding-project.de
;

Seitentyp Dashboard Teil 7 (CMS)

Heute werden wir uns im Seitentyp Dashboard des CMS die einzelnen Widgets in die vor kurzem erzeugten Listen hinzufügen. Dafür müssen wir zu im Template /system/templates/controls/dashboardedit.html die restlichen Schleifen mit Textfeldern ausstatten. Das geht so:

      <h3>Nicht benutzt</h3>
        <ul class="list" id="DONTUSEDWIDGETS">
        {LOOP:DONTUSEDWIDGETS}
          <li style="cursor:move;">
            {VAR:WIDGETNAME}
            <input name="{VAR:WIDGETPATH}" value="{VAR:PLACE}" type="hidden" />
          </li>
        {/LOOP:DONTUSEDWIDGETS}
        </ul>
    </td>
    <td>
      <h3>Versteckt</h3>
      <ul class="list" id="HIDDENWIDGETS">
        {LOOP:HIDDENWIDGETS}
          <li style="cursor:move;">
            {VAR:WIDGETNAME}
            <input name="{VAR:WIDGETPATH}" value="{VAR:PLACE}" type="hidden" />
          </li>
        {/LOOP:HIDDENWIDGETS}
      </ul>
    </td>
  </tr>
  <tr>
    <td>
      <h3>Spalte 1</h3>
      <ul class="list" id="COLUMN1WIDGETS">
        {LOOP:COLUMN1WIDGETS}
          <li style="cursor:move;">
            {VAR:WIDGETNAME}
            <input name="{VAR:WIDGETPATH}" value="{VAR:PLACE}" type="hidden" />
          </li>
        {/LOOP:COLUMN1WIDGETS}
      </ul>
    </td>
    <td>
      <h3>Spalte 2</h3>
      <ul class="list" id="COLUMN2WIDGETS">
        {LOOP:COLUMN2WIDGETS}
          <li style="cursor:move;">
            {VAR:WIDGETNAME}
            <input name="{VAR:WIDGETPATH}" value="{VAR:PLACE}" type="hidden" />
          </li>
        {/LOOP:COLUMN2WIDGETS}
      </ul>
    </td>
    <td>
      <h3>Spalte 3</h3>
      <ul class="list" id="COLUMN3WIDGETS">
        {LOOP:COLUMN3WIDGETS}
          <li style="cursor:move;">
            {VAR:WIDGETNAME}
            <input name="{VAR:WIDGETPATH}" value="{VAR:PLACE}" type="hidden" />
          </li>
        {/LOOP:COLUMN3WIDGETS}
      </ul>

Im /system/classes/dashboard.php selbst wird nun über die Tabelle cms_dashboards auf die einzelnen Spalten zugegriffen. Neu ist die Spalte 0, welche versteckte Widgets beinhaltet. Nicht gebrauchte Widgets werden dadurch festgestellt, in dem alle bisher gebrauchten Widgets in die Variable usedwidgets gespeichert werden. Später werden alle Widgets durchlaufen und es wird nachgeschaut, ob das Widget bereits angezeigt wurde. Das Hinzufügen des Widgets in einer Spalte ist in der neuen Funktion addWidgetToEditable:

<?PHP
  classDashboard extends Editor{
    public $id = 1;
    public $cols;
    public $usedwidgets =null;

    function getHeader(){
      return "<link href="/admin/style.css" rel="stylesheet" type="text/css" />
<link href="/admin/inettuts.css" rel="stylesheet" type="text/css" />";
    }

    function __construct(){
      $this->cols[] = $this->loadWidgetsByColumn(1);
      $this->cols[] = $this->loadWidgetsByColumn(2);
      $this->cols[] = $this->loadWidgetsByColumn(3);
    }

    function loadWidgetsByColumn($columnId){
      $columnId = $GLOBALS['db']->EscapeString($columnId);
      $rows = $GLOBALS['db']->ReadRows("SELECT path FROM {'dbprefix'}dashboards 
WHERE col = '".$columnId."' AND id = '".$this->id."'
ORDER BY row");
      if($rows){
        foreach($rows as $row){
          $widgetData =WidgetController::getWidgetData($row->path);
          $widget =WidgetController::getWidget($widgetData);
          $widget->load();
          $res[] = $widget;
        }
      }

      return $res;
    }

    function display(){
      echo "<div id="columns">";
      $this->displayColumn(0);
      $this->displayColumn(1);
      $this->displayColumn(2);
      ?>
        <script type="text/javascript"src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
        <script type="text/javascript"src="/admin/jquery-ui-personalized-1.6rc2.min.js"></script>
        <script type="text/javascript"src="/admin/inettuts.js"></script>
      <?PHP
      echo "</div>";
    }

    public function getEditableCode(){
      $template = new Template();
      $template->load("control_dashboardedit");
      $template->assign_var("URL",$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
      $widgets =WidgetController::getAllWidgets();
      foreach($widgets as $widget){
        $widget->load();
      }
      $i = 1;
      $pos = 0;
      foreach($this->cols as $col){
        if($col){
          foreach($col as $widget){
            $index = $template->add_loop_item("COLUMN".$i."WIDGETS");
            $this->addWidgetToEditable($template,"COLUMN".$i."WIDGETS",$widget,$index,$pos);
            $pos++;
          }
        }
        $i++;
      }
      $hiddenwidgets = $this->getHiddenWidgets();
      $i = 0;
      foreach($widgets as $widget){
        if(!in_array($widget->path,$this->usedwidgets)){
          $index = $template->add_loop_item("HIDDENWIDGETS");
          $this->addWidgetToEditable($template,"HIDDENWIDGETS",$widget,$index,$i);
          $i++;
        }
      }
      $i = 0;
      foreach($widgets as $widget){
        if(!in_array($widget->path,$this->usedwidgets)){
          $index = $template->add_loop_item("DONTUSEDWIDGETS");
          $this->addWidgetToEditable($template,"DONTUSEDWIDGETS",$widget,$index,$i);
          $i++;
        }
      }
      return $template->getCode();
    }

    private function addWidgetToEditable($template,$column,$widget,$index,$position){
      $template->assign_loop_var($column,$index,"WIDGETNAME",htmlentities($widget->headline));
      $template->assign_loop_var($column,$index,"WIDGETPATH",htmlentities($widget->path));
      $template->assign_loop_var($column,$index,"PLACE",$column."|".$position);
      $this->usedwidgets[] = $widget->path;
      return $template;
    }

    public function save($newPage,$oldPage){
    }

    function displayColumn($id){
      ?>
        <ul id="column<?PHP echo $id; ?>" class="column">
          <?PHP
            if($this->cols[$id]){
              foreach($this->cols[$id] as $widget){
                $widget->displayType = "dashboard";
                $widget->display();
              }
            }
          ?>
        </ul>
      <?PHP
    }

    function getHiddenWidgets(){
      $widgets = $GLOBALS['db']->ReadRows("SELECT class,{'dbprefix'}dashboards.path FROM {'dbprefix'}dashboards INNER JOIN {'dbprefix'}widgets ON {'dbprefix'}dashboards.path = {'dbprefix'}widgets.path WHERE id = '".$GLOBALS['db']->EscapeString($this->id)."' AND col = '0'");
      if($widgets){
        foreach($widgets as $widget){
          $res[] =WidgetController::getWidgetData($widget);
        }
      }
      return $res;
    }
  }
?>