public function saveFieldLinks(array $data, $type) { if (!DataCenterPage::userCan('change')) { return false; } DataCenterWidgetFieldLinks::saveFieldLinks($data); return true; }
public function export(array $data, $type) { if (!DataCenterPage::userCan('export')) { return false; } DataCenterWidgetExport::export($data); return null; }
public static function render(array $parameters) { global $wgUser; // Gets current path $path = DataCenterPage::getPath(); // Sets Defaults $parameters = array_merge(self::$defaultParameters, $parameters); // Begins widget $xmlOutput = parent::begin($parameters['class']); // Adds result type menu $currentTarget = null; $currentNum = null; $menuItems = array(); foreach (self::$targets as $target) { $numMatches = DataCenterDB::numMatches($target['category'], $target['type'], $target['fields'], $parameters['query']); if ($numMatches == 0) { continue; } $fusedType = $target['category'] . '.' . $target['type']; if (!$path['type']) { $path['type'] = $fusedType; } if ($path['type'] == $fusedType) { $currentTarget = $target; $currentNum = $numMatches; $state = 'current'; } else { $state = 'normal'; } $typePath = array_merge($path, array('type' => $target['category'] . '.' . $target['type'])); $menuItems[] = DataCenterXml::div(array('class' => 'type-' . $state), DataCenterXml::link(DataCenterUI::message('results', $target['category'] . '-' . $target['type'], $numMatches), $typePath)); } $resultItems = array(); if (!$currentTarget) { $xmlOutput .= DataCenterUI::renderWidget('body', array('message' => 'notice-no-results', 'style' => 'notice')); } else { $joins = array(); if ($currentTarget['class'] == 'DataCenterDBAsset') { $joins = array_merge_recursive(DataCenterDB::buildJoin('model', $currentTarget['type'], 'id', 'asset', $currentTarget['type'], 'model', array('name', 'manufacturer')), DataCenterDB::buildJoin('facility', 'location', 'id', 'asset', $currentTarget['type'], 'location', array('name' => 'location_name'))); } // Gets search results $results = DataCenterDB::getMatches($currentTarget['class'], $currentTarget['category'], $currentTarget['type'], $currentTarget['fields'], $parameters['query'], array_merge_recursive($joins, DataCenterDB::buildRange($path))); // Adds types $xmlOutput .= DataCenterXml::div(array('class' => 'types'), implode($menuItems)); // Adds results $xmlOutput .= DataCenterXml::div(array('class' => 'results'), DataCenterUI::renderWidget('table', array_merge($currentTarget['table'], array('rows' => $results, 'num' => $currentNum)))); } // Ends widget $xmlOutput .= parent::end(); // Returns results return $xmlOutput; }
public function link(array $data, $type) { if (!DataCenterPage::userCan('change')) { return false; } $link = DataCenterDBModelLink::newFromValues($data['row']); if ($link->get('quantity') == 0) { $link->delete(); } else { $link->save(); } return true; }
public static function render(array $parameters) { // Sets defaults $parameters = array_merge_recursive(self::$defaultParameters, $parameters); // Begins widget $xmlOutput = parent::begin($parameters['class']); // Gets current path $path = DataCenterPage::getPath(); // Begins icons $xmlOutput .= DataCenterXml::open('div', array('class' => 'icons')); // Loops over each row foreach ($parameters['rows'] as $row) { // Sets div attributes $divAttributes = array('class' => 'icon'); // Checks if a list of types was specified if (count($parameters['types']) > 0) { // Adds list of type classes foreach ($parameters['types'] as $type) { $divAttributes['class'] .= ' ' . $type . '-' . $row->get($type); } } else { // Adds generic class $divAttributes['class'] .= ' generic'; } if (is_array($parameters['label'])) { $label = ''; foreach ($parameters['label'] as $key => $value) { $label .= DataCenterXml::div(array('class' => 'label-' . $key), $row->get($value)); } } else { $label = DataCenterXml::div(array('class' => 'label-0'), $row->get($parameters['label'])); } $divAttributes = array_merge($divAttributes, DataCenterXml::buildEffects($parameters['effects'], $row), DataCenterXml::buildLink($parameters['link'], $row)); if (count($parameters['link']) > 0) { $divAttributes['class'] .= ' link'; } // Adds icon $xmlOutput .= DataCenterXml::div($divAttributes, $label); } // Clears floating $xmlOutput .= DataCenterXml::clearFloating(); // Ends icon view $xmlOutput .= DataCenterXml::close('div'); // Ends widget $xmlOutput .= parent::end(); // Returns XML return $xmlOutput; }
public function view($path) { // Checks if the user did not provide enough information if (!$path['id']) { // Returns error message return DataCenterUI::message('error', 'insufficient-data'); } // Gets plan from database $plan = DataCenterDB::getPlan($path['id']); // Gets space of plan $space = $plan->getSpace(); // Gets location of space $location = $space->getLocation(); // Sets location information to space $space->set('location_name', implode(' / ', $location->get(array('name', 'region')))); // Gets structure of plan from database $structure = $plan->getStructure(DataCenterDB::buildSort('link', 'asset', array('x', 'y'))); // Builds tables from structure $tables = DataCenterDB::buildLookupTable('asset_type', $structure); // Builds list of rack/object assets used in plan if (isset($tables['rack']) && is_array($tables['rack'])) { foreach ($tables['rack'] as $key => $link) { $asset = $link->getAsset(); $model = $asset->getModel(); $tables['rack'][$key]->set(array('model' => implode(' / ', $model->get(array('manufacturer', 'name'))))); } } else { $tables['rack'] = null; } // Builds javascript that references the renderable plan $target = array('dataCenter.renderer.getTarget' => array(DataCenterJs::toScalar('plan')), 'getModule'); // Detects if this user came from a zoomed in page $refererPath = DataCenterPage::getRefererPath(); $zoomOptions = array(); if ($refererPath['page'] == $path['page'] && $refererPath['type'] == 'rack' && $refererPath['id'] !== null) { $rackLink = DataCenterDB::getAssetLink($refererPath['id']); $rack = $rackLink->getAsset(); $zoomOptions['zoom-from-rack'] = $rack->getId(); } if (!isset($tables['rack'])) { $tables['rack'] = array(); } // Builds table of racks $racks = DataCenterUI::renderWidget('table', array('rows' => $tables['rack'], 'fields' => array('name', 'model', 'position' => array('fields' => array('x', 'y'), 'glue' => ' x ')), 'link' => array('page' => 'plans', 'type' => 'rack', 'action' => 'view', 'id' => '#id'), 'effects' => array(array('event' => 'onmouseover', 'script' => DataCenterJs::chain(array_merge($target, array('setRackHighlight' => array('{asset_id}', DataCenterJs::toScalar(true)))), false)), array('event' => 'onmouseout', 'script' => DataCenterJs::chain(array_merge($target, array('clearRackHighlight' => array(DataCenterJs::toScalar(true)))), false))))); // Returns single columm layout with a table return DataCenterUI::renderLayout('columns', array(DataCenterUI::renderLayout('rows', array(DataCenterUI::renderWidget('heading', array('message' => 'racks')), $racks, DataCenterUI::renderWidget('actions', array('links' => array(array('page' => 'plans', 'type' => 'rack', 'action' => 'select', 'parameter' => array('plan', $path['id']))), 'rights' => array('change'))), DataCenterUI::renderWidget('heading', array('message' => 'configuration')), DataCenterUI::renderWidget('details', array('row' => $plan, 'fields' => array('tense' => array('format' => 'option'), 'name', 'note'))), DataCenterUI::renderWidget('heading', array('message' => 'facility')), DataCenterUI::renderWidget('details', array('row' => $space, 'fields' => array('tense' => array('format' => 'option'), 'location' => array('field' => 'location_name'), 'name', 'size' => array('fields' => array('width', 'depth', 'height'), 'glue' => ' x '), 'power'))))), DataCenterUI::renderWidget('plan', array_merge(array('plan' => $plan), $zoomOptions)))); }
private static function renderModel($parameters, $structure, $level = 0) { $xmlOutput = ''; foreach ($structure as $model) { $modelLink = DataCenterDB::getModelLink($model->get('link')); if (!DataCenterPage::userCan('change')) { $rowAttributes = array(); } elseif ($level == 0 && count($parameters['link']) > 0) { $rowAttributes = array_merge(array('class' => 'link'), DataCenterXml::buildLink($parameters['link'], $model)); } else { $rowAttributes = array('class' => 'mute'); } $xmlOutput .= DataCenterXml::row($rowAttributes, DataCenterXml::cell(DataCenterXml::div(array('style' => 'padding-left:' . $level * 15 . 'px'), $modelLink->get('name'))) . DataCenterXml::cell(array('align' => 'right'), $modelLink->get('quantity')) . DataCenterXml::cell($model->get('name')) . DataCenterXml::cell(DataCenterUI::message('type', $model->getType()))); $xmlOutput .= self::renderModel($parameters, $model->getStructure(), $level + 1); } return $xmlOutput; }
public static function redirect($data) { global $wgOut; $path = DataCenterPage::getPath(); if (isset($data['meta']['query'])) { // Sanitize: allow alpha-numeric $queryContent = urlencode(preg_replace('`\\ +`', ' ', preg_replace('`[^a-z0-9]`i', '', $data['meta']['query']))); // Sanitize: allow alpha-numeric as well as spaces, underscores, // dashes and periods $query = urlencode(preg_replace('`\\ +`', ' ', preg_replace('`[^a-z0-9\\ \\_\\-\\.]`i', '', $data['meta']['query']))); } if (isset($queryContent) && $queryContent != '') { // Shows search results $path['action'] = 'results'; $path['parameter'] = $query; } $wgOut->redirect(DataCenterXml::url($path)); }
public static function render(array $parameters) { // Sets defaults $parameters = array_merge(self::$defaultParameters, $parameters); // Checks for permissions if (!DataCenterPage::userCan($parameters['rights'])) { return null; } // Begins widget $xmlOutput = parent::begin($parameters['class']); // Checks if links is an array if (is_array($parameters['links'])) { // Loops over each link foreach ($parameters['links'] as $label => $link) { // Checks if link is not an array if (!is_array($link)) { // Skips the invalid data continue; } // Checks if a label was not given but an action was if (is_int($label) && isset($link['action'])) { // Uses action as label $label = $link['action']; } if (is_array($link[$parameters['subject']])) { $subject = current($link[$parameters['subject']]); } else { $subject = $link[$parameters['subject']]; } // Builds label $label = DataCenterUI::message('action', $label . '-type', DataCenterUI::message('type', $subject)); // Builds link $link = DataCenterXml::link($label, $link); // Adds action link $xmlOutput .= DataCenterXml::div(array('class' => 'action'), $link); } } // Ends widget $xmlOutput .= parent::end(); $xmlOutput .= DataCenterXml::clearFloating(); // Returns results return $xmlOutput; }
public function view($path) { // Checks if the user did not provide enough information if (!$path['id']) { // Returns error message return DataCenterUI::message('error', 'insufficient-data'); } // Gets link from database $rackLink = DataCenterDB::getAssetLink($path['id']); // Extracts rack from link $rack = $rackLink->getAsset(); // Gets plan from database $plan = DataCenterDB::getPlan($rackLink->get('plan')); // Gets structure of plan $structure = $plan->getStructure(DataCenterDB::buildSort('link', 'asset', array('orientation', 'z DESC'))); // Gets objects from rack in structure $id = $rackLink->getId(); $objectLinks = array(); foreach ($structure as $rackLink) { if ($rackLink->getId() == $id) { $objectLinks = $rackLink->getStructure(); break; } } foreach ($objectLinks as $key => $objectLink) { $object = $objectLink->getAsset(); $objectModel = $object->getModel(); $objectLinks[$key]->set(array('rack' => $object->getId(), 'model' => implode(' / ', $objectModel->get(array('manufacturer', 'name'))), 'position' => implode(' / ', array($objectLink->get('z'), DataCenterUI::format($objectLink->get('orientation'), 'side'))))); } // Builds javascript that references the renderable asset $target = DataCenterJs::chain(array('dataCenter.renderer.getTarget' => array(DataCenterJs::toScalar('plan')), 'getModule'), false); // Detects if this user came from a zoomed out page $refererPath = DataCenterPage::getRefererPath(); $zoomOptions = array(); if ($refererPath['page'] != $path['page'] || $refererPath['type'] == 'plan') { $zoomOptions['zoom-to-rack'] = $rack->getId(); } else { $zoomOptions['look-at-rack'] = $rack->getId(); } // Returns single columm layout with a table return DataCenterUI::renderLayout('columns', array(DataCenterUI::renderLayout('rows', array(DataCenterUI::renderWidget('heading', array('message' => 'objects')), 'objects' => DataCenterUI::renderWidget('table', array('rows' => $objectLinks, 'fields' => array('name', 'model', 'position'), 'link' => array('page' => 'plans', 'type' => 'object', 'id' => '#id', 'action' => 'view'), 'effects' => array(array('event' => 'onmouseover', 'script' => $target . '.setObjectHighlight({rack},true);', 'field' => 'id'), array('event' => 'onmouseout', 'script' => $target . '.clearObjectHighlight( true );')))), DataCenterUI::renderWidget('actions', array('links' => array(array('page' => 'plans', 'type' => 'object', 'action' => 'select', 'parameter' => array('rack', $path['id']))), 'rights' => array('change'))), DataCenterUI::renderWidget('heading', array('message' => 'configuration')), DataCenterUI::renderWidget('details', array('row' => $rackLink, 'fields' => array('position' => array('fields' => array('x', 'y'), 'glue' => ' x '), 'orientation' => array('format' => 'angle')))), DataCenterUI::renderWidget('heading', array('message' => 'asset')), DataCenterUI::renderWidget('details', array('row' => $rack, 'fields' => array('serial', 'asset'))))), DataCenterUI::renderWidget('plan', array_merge(array('plan' => $plan, 'current-rack' => $rack->getId()), $zoomOptions)))); }
public function save(array $data, $type) { if (!DataCenterPage::userCan('change')) { return false; } switch ($type) { case 'location': $component = DataCenterDBLocation::newFromValues($data['row']); break; case 'space': $component = DataCenterDBSpace::newFromValues($data['row']); break; } if (isset($component)) { $component->save(); $component->saveMetaValues($data['meta']); $component->insertChange($data['change']); return true; } return false; }
public static function compareChanges($data) { global $wgOut; $path = DataCenterPage::getPath(); $path['parameter'] = array($data['meta']['change1'], $data['meta']['change2']); $wgOut->redirect(DataCenterXml::url($path)); }
public function execute($sub) { global $wgOut, $wgScriptPath, $wgUser, $wgRequest; // Checks if the user is logged in if (!$wgUser->isLoggedIn()) { // Lets them know they need to $wgOut->loginToUse(); // Returns true so MediaWiki can move on return true; } // Gets user rights self::$rights = $wgUser->getRights(); if (!self::userCan('view')) { $wgOut->permissionRequired('datacenter-view'); return true; } // Keeps some state between pages self::loadState(); // Begins output $this->setHeaders(); // Checks if the path is empty if ($sub == '') { // Uses the first page in the array as a default $sub = current(array_keys(self::$pages)); } // Gets path from sub $path = self::subToPath($sub); // Verifies page name and parameters if (isset(self::$pages[$path['page']])) { // Makes shortcut to page classes $pageClasses = self::$pages[$path['page']]; // Makes shortcut to controller class $controllerClass = $pageClasses['controller']; // Verifies existence of page class if (class_exists($controllerClass)) { // Creates instance of page class $controller = new $controllerClass($path); // Checks if the view is empty if ($path['action'] === null) { // Uses default action $path['action'] = 'main'; } // Checks if the type is empty if ($path['type'] === null) { // Checks if default type is available if (isset(self::$pages[$path['page']]['default'])) { // Uses default type $path['type'] = self::$pages[$path['page']]['default']; } } // Verifies instance inherited the correct parent class if ($controller instanceof DataCenterController) { // Verifies the current edit token matches any passed token $token = $wgRequest->getText('token'); if ($wgUser->matchEditToken($token)) { // Gets the action to be performed $do = $wgRequest->getText('do'); // Verifies handler is not the instance's constructor and // that action exists in the instance if ($do !== '__constructor' && is_callable(array($controllerClass, $do))) { if ($wgRequest->getCheck('cancel')) { // Redirects to success URL without acting $wgOut->redirect($wgRequest->getText('cancellation')); } else { // Gets submitted data $data = array('row' => $wgRequest->getArray('row'), 'meta' => $wgRequest->getArray('meta'), 'change' => $wgRequest->getArray('change')); // Delegates handling of form submissions $status = $controller->{$do}($data, $path['type']); // Checks if status is not null if ($status !== null) { // Redirects to success or failure URL $wgOut->redirect($wgRequest->getText($status ? 'success' : 'failure')); } } } } // Sets destinations for menus DataCenterUI::setDestinations(self::$pages, $controller, $path); $viewClass = null; // Checks if specialized view exists if (isset($pageClasses['types'][$path['type']])) { // Uses specialized view $viewClass = $pageClasses['types'][$path['type']]; // Verifies specialized view can handle action $renderFunction = array($viewClass, $path['action']); if (!is_callable($renderFunction)) { $viewClass = null; } } // Checks if a view class has been not selected yet if (!$viewClass) { // Uses generic view $viewClass = $pageClasses['view']; // Verifies generic view can handle action $renderFunction = array($viewClass, $path['action']); if (!is_callable($renderFunction)) { $viewClass = null; } } // Verifies page user interface class exists if ($viewClass && class_exists($viewClass)) { // Creates instance of page user interface class $view = new $viewClass($controller); // Verifies the action is not the instance's constructor if ($path['action'] !== '__constructor') { // Renders view of instance $renderFunction = array($view, $path['action']); DataCenterUI::addContent(call_user_func($renderFunction, $path)); } } } } } DataCenterUI::render(); // Keeps some state between pages self::saveState(); }
public static function render(array $parameters) { // Sets defaults $parameters = array_merge_recursive(self::$defaultParameters, $parameters); // Begins widget $xmlOutput = parent::begin($parameters['class']); // Processes fields $fields = self::processFields($parameters['fields']); // Check if there was an error processing fields if (!$fields) { // Tells the user return DataCenterUI::message('error', 'no-fields'); } // Begins table $xmlOutput .= DataCenterXml::open('table', array('border' => 0, 'cellspacing' => 0)); // Gets current path $path = DataCenterPage::getPath(); // Check if number of possible records was given if (isset($parameters['num'])) { // Adds paging $xmlOutput .= DataCenterXml::row(DataCenterXml::cell(array('colspan' => count($parameters['fields']), 'align' => 'right'), parent::buildPaging($path, $parameters['num']))); } // Adds headings foreach ($fields as $label => $options) { $xmlOutput .= DataCenterXml::headingCell(array('align' => $options['align']), DataCenterUI::message('field', $label)); } if (count($parameters['rows']) == 0) { $xmlOutput .= DataCenterXml::row(DataCenterXml::cell(DataCenterUI::message('error', 'no-rows'))); } // Loops over each row foreach ($parameters['rows'] as $i => $row) { // Builds row attributes $rowAttributes = array_merge(array('class' => $i % 2 == 0 ? 'odd' : 'even'), DataCenterXml::buildEffects($parameters['effects'], $row), DataCenterXml::buildLink($parameters['link'], $row)); if (count($parameters['link']) > 0) { $rowAttributes['class'] .= ' link'; } // Begins row $xmlOutput .= DataCenterXml::open('tr', $rowAttributes); // Loops over each field foreach ($fields as $label => $options) { // Checks if multiple fields were specified if (count($options['fields']) > 0) { // Builds array of values $values = array(); foreach ($options['fields'] as $field => $fieldOptions) { $values[] = DataCenterUI::format($row->get($field), $fieldOptions['format']); } // Glues values together $value = implode($options['glue'], $values); // Alternatively checks if a field was specified } elseif ($options['field']) { // Uses specified field $value = DataCenterUI::format($row->get($options['field']), $options['format']); } else { // Uses label as field $value = DataCenterUI::format($row->get($label), $options['format']); } // Adds cell $xmlOutput .= DataCenterXml::cell(array('align' => $options['align']), $value); } // Ends row $xmlOutput .= DataCenterXml::close('tr'); } // Ends table $xmlOutput .= DataCenterXml::close('table'); // Clears any floating $xmlOutput .= DataCenterXml::clearFloating(); // Ends widget $xmlOutput .= parent::end(); // Returns results return $xmlOutput; }
public function save(array $data, $type) { if (!DataCenterPage::userCan('change')) { return false; } switch ($type) { case 'plan': $plan = DataCenterDBPlan::newFromValues($data['row']); $plan->save(); return true; case 'rack': case 'object': $link = DataCenterDBAssetLink::newFromValues($data['row']); $link->save(); return true; } return false; }
public static function render(array $parameters) { global $wgUser; // Gets current path $path = DataCenterPage::getPath(); // Sets Defaults $parameters = array_merge(self::$defaultParameters, $parameters); // Begins widget $xmlOutput = parent::begin($parameters['class']); // Gets existing links from database $metaFieldLinks = $parameters['field']->getLinks(); $existsTable = array(); foreach ($metaFieldLinks as $metaFieldLink) { $key = implode('_', $metaFieldLink->get(array('component_category', 'component_type'))); $existsTable[$key] = $metaFieldLink; } // Builds form attributes $formAttributes = array('id' => 'form_fieldlinks', 'name' => 'form_fieldlinks', 'method' => 'post', 'action' => DataCenterXml::url($path)); // Begins form $xmlOutput .= DataCenterXml::open('form', $formAttributes); // Begins table $xmlOutput .= DataCenterXml::open('table', self::$defaultAttributes['table']); foreach (self::$targets as $category => $types) { $xmlOutput .= DataCenterXml::row(DataCenterXml::headingCell(self::$defaultAttributes['heading'], DataCenterUI::message('category', $category))); foreach ($types as $type) { $name = $category . '_' . $type; $checkboxAttributes = array('type' => 'checkbox', 'name' => "meta[{$name}]", 'id' => "field_{$name}", 'value' => 1); $count = 0; if (isset($existsTable[$name])) { $checkboxAttributes['checked'] = 'checked'; $count = $existsTable[$name]->numValues(); if ($count > 0) { $checkboxAttributes['disabled'] = 'true'; $checkboxAttributes['name'] = "locked[{$name}]"; $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => "meta[{$name}]", 'value' => 1)); } } if (!DataCenterPage::userCan('change')) { $checkboxAttributes['disabled'] = 'true'; } $xmlOutput .= DataCenterXml::row(DataCenterXml::cell(self::$defaultAttributes['checkbox'], DataCenterXml::tag('input', $checkboxAttributes)), DataCenterXml::cell(self::$defaultAttributes['label'], DataCenterXml::tag('label', array('for' => "field_{$name}"), DataCenterUI::message('type', $type))), DataCenterXml::cell(self::$defaultAttributes['uses'], $count > 0 ? DataCenterUI::message('label', 'num-uses', $count) : '')); } } if (DataCenterPage::userCan('change')) { // Adds reset and submit button $xmlOutput .= DataCenterXML::row(DataCenterXml::cell(self::$defaultAttributes['buttons'], DataCenterXml::tag('input', array('type' => 'reset', 'name' => 'reset', 'class' => 'reset', 'value' => DataCenterUI::message('label', 'reset'))) . DataCenterXml::tag('input', array('type' => 'submit', 'name' => 'submit', 'class' => 'submit', 'value' => DataCenterUI::message('label', 'save'))))); } $xmlOutput .= DataCenterXml::close('table'); // Adds row fields $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'row[id]', 'value' => $parameters['field']->getId())); // Adds do field $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'do', 'value' => 'saveFieldLinks')); // Adds token field $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken())); // Adds success field $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'success', 'value' => DataCenterXml::url($path))); // Adds failure field $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'failure', 'value' => DataCenterXml::url($path))); // Adds canellation field $xmlOutput .= DataCenterXml::tag('input', array('type' => 'hidden', 'name' => 'cancellation', 'value' => DataCenterXml::url($path))); $xmlOutput .= DataCenterXml::close('form'); // Ends widget $xmlOutput .= parent::end(); // Returns results return $xmlOutput; }
public static function export(array $data) { global $wgOut; // Disables MediaWiki output $wgOut->disable(); // Gets current path $path = DataCenterPage::getPath(); // Gets time in a nice format $date = date('Y-m-d'); $fileName = DataCenterUI::message('datacenter') . ' - ' . DataCenterUI::message('label', 'export-type', DataCenterUI::message('type', $data['meta']['type']) . ' ' . DataCenterUI::message('category', $data['meta']['category'])) . ' - ' . $date . '.' . $data['meta']['format']; // Sets headers for downloading CSV file header('Content-type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"{$fileName}\""); $rows = DataCenterDB::getRows('DataCenterDBRow', $data['meta']['category'], $data['meta']['type']); $useMeta = $data['meta']['include-meta'] == 1 && ($data['meta']['category'] == 'facility' && DataCenterDB::isFacilityType($data['meta']['type']) || $data['meta']['category'] == 'asset' && DataCenterDB::isAssetType($data['meta']['type']) || $data['meta']['category'] == 'model' && DataCenterDB::isModelType($data['meta']['type'])); if ($data['meta']['format'] == 'csv') { $metaFieldsTable = null; $lines = array(); $fieldNames = ''; $first = true; foreach ($rows as $row) { $line = ''; $fields = $row->get(); foreach ($fields as $field => $value) { $line .= self::exportValue($value); } if ($first) { foreach ($fields as $field => $value) { $fieldNames .= self::exportValue(DataCenterUI::message('field', $field)); } } if ($useMeta) { $component = DataCenterDBComponent::newFromClass('DataCenterDBComponent', $data['meta']['category'], $data['meta']['type'], $fields); if (!$metaFieldsTable) { $metaFields = $component->getMetaFields(); $metaFieldsTable = array(); foreach ($metaFields as $metaField) { $metaFieldsTable[] = $metaField->get('field'); } if ($first) { foreach ($metaFields as $metaField) { $fieldNames .= self::exportValue($metaField->get('name')); } } } $metaValues = $component->getMetaValues(); $metaValuesTable = array(); foreach ($metaValues as $metaValue) { $metaValuesTable[$metaValue->get('field')] = $metaValue->get('value'); } foreach ($metaFieldsTable as $metaField) { if (isset($metaValuesTable[$metaField])) { $line .= self::exportValue($metaValuesTable[$metaField]); } else { $line .= ','; } } } $lines[] = rtrim($line, ',') . "\r\n"; $first = false; } echo rtrim($fieldNames, ',') . "\r\n"; echo implode($lines); } }