} $columnlayoutoptions = array(); $columnlayouts = get_records_assoc('view_layout_columns'); foreach ($columnlayouts as $layout => $percents) { $percentswidths = str_replace(',', ' - ', $percents->widths); $columnlayoutoptions[$layout] = $percentswidths; } // provide a simple default to build custom layouts with $defaultcustomlayout = View::default_columnsperrow(); $defaultlayout = get_record('view_layout_columns', 'columns', $defaultcustomlayout[1]->columns, 'widths', $defaultcustomlayout[1]->widths); $clnumcolumnsdefault = $defaultlayout->columns; $clwidths = $defaultlayout->widths; // Ready custom layout preview. $defaultlayoutpreviewdata['layout']['row1'] = $defaultcustomlayout[1]->widths; $defaultlayoutpreviewdata['text'] = get_string($defaultcustomlayout[1]->widths, 'view'); $defaultlayoutpreview = new LayoutPreviewImage($defaultlayoutpreviewdata); $inlinejavascript = <<<JAVASCRIPT function get_max_custom_rows() { return {$maxlayoutrows}; } addLoadEvent(function () { formchangemanager.add("viewlayout"); }); JAVASCRIPT; $elements = array('viewid' => array('type' => 'hidden', 'value' => $view->get('id'))); $elements['customlayoutnumrows'] = array('type' => 'hidden', 'value' => 1); $elements['layoutselect'] = array('type' => 'hidden', 'value' => $currentlayout, 'sesskey' => $USER->get('sesskey')); $elements['layoutfallback'] = array('type' => 'hidden', 'value' => $defaultlayout->id);
public function addcustomlayout($values) { require_once get_config('libroot') . 'layoutpreviewimage.php'; $require = array('numrows'); foreach ($require as $require) { if (!array_key_exists($require, $values) || empty($values[$require])) { throw new ParamOutOfRangeException(get_string('missingparam' . $require, 'error')); } } $numrows = $values['numrows']; $alttext = ''; $rowscolssql = ''; $rowscols = array(); for ($i = 0; $i < $numrows; $i++) { if (array_key_exists('row' . ($i + 1), $values)) { $rowscolssql .= '(row = ' . ($i + 1) . ' AND columns = ' . $values['row' . ($i + 1)] . ')'; if ($i != $numrows - 1) { $rowscolssql .= ' OR '; } $widths = get_field('view_layout_columns', 'widths', 'id', $values['row' . ($i + 1)]); $hyphenatedwidths = str_replace(',', '-', $widths); $alttext .= $hyphenatedwidths; if ($i != $numrows - 1) { $alttext .= ' / '; } $rowscols[$i + 1] = $values['row' . ($i + 1)]; } } $owner = $this->owner; $group = $this->group; $institution = $this->institution; if (!empty($group)) { $owner = null; $andclause = 'AND ucl.group = ?'; $andclausevalue = $group; } else { if (!empty($institution)) { $owner = null; $andclause = 'AND ucl.institution = ?'; $andclausevalue = $institution; } else { if (isset($owner)) { $andclause = 'AND ucl.usr = ?'; $andclausevalue = $owner; } else { // no group or owner or institution set // site pages should have institution set throw new SystemException("View::addcustomlayout: No owner, group or institution set for view."); } } } // check for existing layout $sql = 'SELECT vlrc.viewlayout AS id FROM {view_layout} vl INNER JOIN {view_layout_rows_columns} vlrc ON vl.id = vlrc.viewlayout INNER JOIN ( SELECT viewlayout, COUNT(*) FROM {view_layout_rows_columns} GROUP BY viewlayout HAVING COUNT(*) = ? ) vlrc2 ON vlrc.viewlayout = vlrc2.viewlayout INNER JOIN {usr_custom_layout} ucl ON ucl.layout = vl.id WHERE (' . $rowscolssql . ') AND ( vl.iscustom = 0 OR ( vl.iscustom = 1 ' . $andclause . ' ) ) GROUP BY vlrc.viewlayout HAVING count(*) = ? LIMIT 1'; $layoutids = get_records_sql_array($sql, array($numrows, $andclausevalue, $numrows)); if ($layoutids) { $data = array('layoutid' => $layoutids[0]->id, 'newlayout' => 0); return $data; } else { db_begin(); // no existing layout of this kind, create it $newlayoutid = insert_record('view_layout', (object) array('rows' => $numrows, 'iscustom' => 1), 'id', true); if (!$newlayoutid) { db_rollback(); throw new SystemException("View::addcustomlayout: Couldn't create new layout record."); } if (isset($owner)) { $newcustomlayout = insert_record('usr_custom_layout', (object) array('usr' => $owner, 'group' => null, 'institution' => null, 'layout' => $newlayoutid)); } else { if (isset($group)) { $newcustomlayout = insert_record('usr_custom_layout', (object) array('usr' => null, 'group' => $group, 'institution' => null, 'layout' => $newlayoutid)); } else { if (isset($institution)) { $newcustomlayout = insert_record('usr_custom_layout', (object) array('usr' => null, 'group' => null, 'institution' => $institution, 'layout' => $newlayoutid)); } } } if (!$newcustomlayout) { db_rollback(); throw new SystemException("View::addcustomlayout: Couldn't create new usr custom layout record."); } for ($i = 0; $i < $numrows; $i++) { if (array_key_exists($i + 1, $rowscols)) { $widths = get_field('view_layout_columns', 'widths', 'id', $rowscols[$i + 1]); $structure['layout']['row' . ($i + 1)] = $widths; $newrec = insert_record('view_layout_rows_columns', (object) array('viewlayout' => $newlayoutid, 'row' => $i + 1, 'columns' => $rowscols[$i + 1])); if (!$newrec) { db_rollback(); throw new SystemException("View::addcustomlayout: Couldn't create new vlrc record."); } } } db_commit(); // Generate new custom layout preview. $structure['text'] = $alttext; $layoutpreview = new LayoutPreviewImage($structure); $preview = $layoutpreview->create_preview(); $data = array('layoutid' => $newlayoutid, 'newlayout' => 1, 'layoutpreview' => $preview, 'text' => $structure['text']); return $data; } }
public function updatecustomlayoutpreview($values) { global $THEME; require_once get_config('libroot') . 'layoutpreviewimage.php'; $require = array('numrows'); foreach ($require as $require) { if (!array_key_exists($require, $values) || empty($values[$require])) { throw new ParamOutOfRangeException(get_string('missingparam' . $require, 'error')); } } $numrows = $values['numrows']; $collayouts = array(); for ($i = 0; $i < $numrows; $i++) { if (array_key_exists('row' . ($i + 1), $values)) { $collayouts['row' . ($i + 1)] = $values['row' . ($i + 1)]; } } $previewimage = 'vl-'; $alttext = ''; $customlayout = array(); for ($i = 0; $i < $numrows; $i++) { $id = $collayouts['row' . ($i + 1)]; $widths = get_field('view_layout_columns', 'widths', 'id', $id); $hyphenatedwidths = str_replace(',', '-', $widths); $customlayout[$i + 1] = $hyphenatedwidths; $previewimage .= $hyphenatedwidths; $alttext .= $hyphenatedwidths; if ($i != $numrows - 1) { $previewimage .= '_'; $alttext .= ' / '; } } if (LayoutPreviewImage::preview_exists($previewimage)) { $img = get_config('wwwroot') . 'thumb.php?type=customviewlayout&cvl=' . $previewimage; $data = array('data' => $img, 'alttext' => $alttext, 'newimage' => 0); return $data; } else { // generate thumbnail images with GD $data = array(); $data['layout'] = $customlayout; $data['description'] = 'test'; $data['owner'] = 1; $previewlayoutimage = new LayoutPreviewImage($data); $newpreviewimage = $previewlayoutimage->create_preview(); if ($newpreviewimage) { $img = get_config('wwwroot') . 'thumb.php?type=customviewlayout&cvl=' . $previewimage; $data = array('data' => $img, 'alttext' => $alttext, 'newimage' => 1); return $data; } else { $msg = '<p>' . get_string('previewimagegenerationfailed', 'error') . '</p>'; $data = array('data' => $msg, 'alttext' => $alttext, 'newimage' => 0); return $data; } } }