Created
July 25, 2015 17:28
-
-
Save Boorj/dbbfb02af1cd94c6296f to your computer and use it in GitHub Desktop.
Bolt's Mass Edit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class General extends AsyncBase | |
protected function addRoutes(ControllerCollection $c) | |
{ | |
//... | |
$c->get('/massedit/read/{contenttypeslug}', 'readMassEdit') | |
->value('data_type', 'values') | |
->bind('readmassedit'); | |
//... | |
} | |
public function readMassEdit(Request $request, $contenttypeslug) | |
{ | |
// Make sure the user is allowed to see this page, based on 'allowed contenttypes' | |
// for Editors. | |
try { | |
if (!$this->isAllowed('contenttype:' . $contenttypeslug)) | |
throw new AccessDeniedException( \Bolt\Translation\Translator::__('You do not have the right privileges to view that page.') ); | |
$contenttype = $this->getContentType($contenttypeslug); | |
if ($contenttype === false) | |
throw new NotFoundResourceException( \Bolt\Translation\Translator::__('No such contenttype'), 404 ); | |
$contenttypeslug = $contenttype['slug']; | |
if (false) { | |
$contentparameters = []; | |
$contentparameters['paging'] = false; | |
$contentparameters['hydrate'] = false; | |
$contentparameters['limit'] = 9999; // todo check if it could be reduced | |
$contentparameters['order'] = $request->query->get('order', $contenttype['sort']); | |
$contentparameters['page'] = $request->query->get('page'); | |
$contentparameters['status'] = 'draft'; | |
$rows = $this->getContent($contenttypeslug, $contentparameters); | |
} | |
else{ | |
$q = [ 'table' => 'bolt_' . $contenttype['tablename'], | |
'order' => "ORDER BY datepublish DESC", ]; | |
$statement = sprintf( 'SELECT * from %s %s', $q['table'], $q['order']); | |
$rows = $this->app['db']->executeQuery($statement)->fetchAll(); | |
} | |
// dump($contenttype); | |
// dump($rows); | |
// die('not'); | |
$clean_contenttype = [ | |
'name' => $contenttype['name'], | |
'fields' => [], | |
]; | |
foreach ($contenttype['fields'] as $field_name => $field) { | |
// taking aboard only fields with mass_edit : true | |
// Todo take aboard also by default - numbers and texts if mass_edit is not set to false | |
if (empty($field['mass_edit'])) | |
continue; | |
$clean_field = [ | |
'label' => $field['label'], | |
'type' => $field['type'], | |
'default' => $field['default'], | |
]; | |
$clean_contenttype['fields'][$field_name] = $clean_field; | |
} | |
// Gathering rows | |
$clean_rows = []; | |
foreach ($rows as $record) { | |
$filtered_record = []; | |
foreach ($contenttype['fields'] as $field_name => $field) { | |
if (!isset($clean_contenttype['fields'][$field_name])) | |
continue; | |
if (!isset($record[$field_name])){ | |
throw new RuntimeException('Database is outdated'); // strange shit happened | |
} | |
// if this field goes to mass edit - add it to the resulting array | |
$filtered_record[$field_name] = $record[$field_name]; | |
} | |
//todo add date_publish, status and some other interesting fields. | |
$clean_rows[] = $filtered_record; | |
} | |
// Gathering lists | |
$lists = []; | |
foreach ($contenttype['fields'] as $field_name => $field) { | |
if ($field['type'] == 'select'){ | |
if (!isset($field['values'])) | |
throw new RuntimeException('no values set for ' . $field_name); | |
if (is_array($field['values'])) { | |
$lists[$field_name] = $field['values']; | |
} | |
else { | |
if (!is_string($field['values'])) | |
throw new RuntimeException("Field $field_name in $contenttypeslug has to be a string or an array.", 500); | |
if (strpos($field['values'], '/') === false) | |
throw new RuntimeException("Field $field_name in $contenttypeslug has to have '/' char.", 500); | |
list($lookuptype, $lookupfields) = explode('/', $field['values']); | |
if (empty($lookuptype) || empty($lookupfields)) | |
throw new RuntimeException("Field $field_name in $contenttypeslug (value = ". strip_tags($field['values']). ") has to have both LOOKUPTYPE and LOOKUPFIELDS ", 500); | |
$lookuptype_content = $this->getContentType($lookuptype); | |
if (false === $lookuptype_content) | |
throw new RuntimeException("Field $field_name in $contenttypeslug has unexisting LOOKUPTYPE (= $lookuptype)", 500); | |
if (strpos($field['values'], ',') !== false) | |
$lookupfields = explode(',', $lookupfields); | |
else | |
$lookupfields = [$lookupfields]; | |
foreach ($lookupfields as $lookupfield) | |
if ($lookupfield !== 'id' && !isset($lookuptype_content['fields'][$lookupfield])) | |
throw new RuntimeException("Field $field_name in $contenttypeslug has unexisting LOOKUPFIELD (= $lookupfield)", 500); | |
$q=[]; | |
// Get fields | |
$q['fields'] = implode(', ', $lookupfields); | |
// Get table | |
$q['table'] = 'bolt_' . $lookuptype_content['tablename']; | |
// Get Sorting | |
$sorting_order = !empty($field['sort'] ) ? $field['sort'] : $lookupfields[0]; | |
list($sorting_order, $sorting_direction) = $this->app['storage']->getSortOrder($sorting_order); | |
$q['sort'] = "ORDER BY " . $sorting_order . " " . $sorting_direction ? 'ASC' : 'DESC'; | |
// Get "where" //todo put here where filtering (if it worth) | |
$where_filter = !empty($field['filter']) ? $field['filter']: ['status']; | |
$q['where'] = ''; | |
$lookups_statement = sprintf( 'SELECT %s from %s%s', | |
$q['fields'], | |
$q['table'], | |
$q['where'], | |
$q['sort'] | |
); | |
$lookups = $this->app['db']->executeQuery($lookups_statement)->fetchAll(); | |
$lists[$field_name] = $lookups; | |
} | |
} | |
} | |
$data = [ | |
'records' => $clean_rows, | |
'contenttype' => $clean_contenttype, | |
'lists' => $lists, | |
]; | |
$response = [ | |
'success' => true, | |
'message' => "OK", | |
'code' => 200, | |
'data' => $data, | |
]; | |
} | |
catch (\Exception $err){ | |
$response = [ | |
'success' => false, | |
'message' => "Error {$err->getCode()}: {$err->getMessage()}", | |
'code' => $err->getCode(), | |
]; | |
} | |
return new \Symfony\Component\HttpFoundation\JsonResponse($response); | |
} |
Author
Boorj
commented
Jul 25, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment