function showTabSensorsAction() { $visit = PortSensorApplication::getVisit(); $translate = DevblocksPlatform::getTranslationService(); $active_worker = PortSensorApplication::getActiveWorker(); $tpl = DevblocksPlatform::getTemplateService(); $tpl->assign('path', $this->_TPL_PATH); // Select tab // $visit->set(PortSensorVisit::KEY_HOME_SELECTED_TAB, 'sensors'); // My Notifications $sensorsView = Ps_AbstractViewLoader::getView(self::VIEW_ACTIVE_SENSORS); // $title = vsprintf($translate->_('home.my_notifications.view.title'), $active_worker->getName()); if (null == $sensorsView) { $sensorsView = new Ps_SensorView(); $sensorsView->id = self::VIEW_ACTIVE_SENSORS; // $sensorsView->name = $title; $sensorsView->renderLimit = 25; $sensorsView->renderPage = 0; $sensorsView->renderSortBy = SearchFields_Sensor::NAME; $sensorsView->renderSortAsc = 1; } // Overload criteria $sensorsView->name = 'Active Sensors'; $sensorsView->params = array(SearchFields_Sensor::IS_DISABLED => new DevblocksSearchCriteria(SearchFields_Sensor::IS_DISABLED, '=', 0)); /* * [TODO] This doesn't need to save every display, but it was possible to * lose the params in the saved version of the view in the DB w/o recovery. * This should be moved back into the if(null==...) check in a later build. */ Ps_AbstractViewLoader::setView($sensorsView->id, $sensorsView); $tpl->assign('view', $sensorsView); $tpl->display('file:' . $this->_TPL_PATH . 'home/tabs/sensors/index.tpl'); }
function doWorkersBulkUpdateAction() { // Checked rows @($ids_str = DevblocksPlatform::importGPC($_REQUEST['ids'], 'string')); $ids = DevblocksPlatform::parseCsvString($ids_str); // Filter: whole list or check @($filter = DevblocksPlatform::importGPC($_REQUEST['filter'], 'string', '')); // View @($view_id = DevblocksPlatform::importGPC($_REQUEST['view_id'], 'string')); $view = Ps_AbstractViewLoader::getView($view_id); // Worker fields @($is_disabled = trim(DevblocksPlatform::importGPC($_POST['is_disabled'], 'string', ''))); $do = array(); // Do: Disabled if (0 != strlen($is_disabled)) { $do['is_disabled'] = $is_disabled; } // Do: Custom fields $do = DAO_CustomFieldValue::handleBulkPost($do); $view->doBulkUpdate($filter, $do, $ids); $view->render(); return; }
function viewRunNowAction() { @($view_id = DevblocksPlatform::importGPC($_REQUEST['view_id'], 'string')); @($ids = DevblocksPlatform::importGPC($_REQUEST['row_id'], 'array')); $sensor_types = DevblocksPlatform::getExtensions('portsensor.sensor', true); if (is_array($ids) && !empty($ids)) { try { $sensors = DAO_Sensor::getWhere(sprintf("%s IN (%s)", DAO_Sensor::ID, implode(',', $ids))); if (is_array($sensors)) { foreach ($sensors as $sensor) { if (isset($sensor_types[$sensor->extension_id])) { // Skip external sensors if ('sensor.external' == $sensor->extension_id) { continue; } $runner = $sensor_types[$sensor->extension_id]; // [TODO] This duplicates cron if (method_exists($runner, 'run')) { $fields = array(); $success = $runner->run($sensor, $fields); $fields[DAO_Sensor::UPDATED_DATE] = time(); $fields[DAO_Sensor::FAIL_COUNT] = $success ? 0 : intval($sensor->fail_count) + 1; DAO_Sensor::update($sensor->id, $fields); } } } } } catch (Exception $e) { // ... } } $view = Ps_AbstractViewLoader::getView($view_id); $view->render(); return; }
function viewDoExportAction() { @($view_id = DevblocksPlatform::importGPC($_REQUEST['view_id'])); @($columns = DevblocksPlatform::importGPC($_REQUEST['columns'], 'array', array())); @($export_as = DevblocksPlatform::importGPC($_REQUEST['export_as'], 'string', 'csv')); // Scan through the columns and remove any blanks if (is_array($columns)) { foreach ($columns as $idx => $col) { if (empty($col)) { unset($columns[$idx]); } } } $view = Ps_AbstractViewLoader::getView($view_id); $column_manifests = $view->getColumns(); // Override display $view->view_columns = $columns; $view->renderPage = 0; $view->renderLimit = -1; if ('csv' == $export_as) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: text/plain; charset=" . LANG_CHARSET_CODE); // Column headers if (is_array($columns)) { $cols = array(); foreach ($columns as $col) { $cols[] = sprintf("\"%s\"", str_replace('"', '\\"', mb_convert_case($column_manifests[$col]->db_label, MB_CASE_TITLE))); } echo implode(',', $cols) . "\r\n"; } // Get data list($results, $null) = $view->getData(); if (is_array($results)) { foreach ($results as $row) { if (is_array($row)) { $cols = array(); if (is_array($columns)) { foreach ($columns as $col) { $cols[] = sprintf("\"%s\"", str_replace('"', '\\"', $row[$col])); } } echo implode(',', $cols) . "\r\n"; } } } } elseif ('xml' == $export_as) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: text/plain; charset=" . LANG_CHARSET_CODE); $xml = simplexml_load_string("<results/>"); /* @var $xml SimpleXMLElement */ // Get data list($results, $null) = $view->getData(); if (is_array($results)) { foreach ($results as $row) { $result =& $xml->addChild("result"); if (is_array($columns)) { foreach ($columns as $col) { $field =& $result->addChild("field", htmlspecialchars($row[$col], null, LANG_CHARSET_CODE)); $field->addAttribute("id", $col); } } } } // Pretty format and output $doc = new DOMDocument('1.0'); $doc->preserveWhiteSpace = false; $doc->loadXML($xml->asXML()); $doc->formatOutput = true; echo $doc->saveXML(); } exit; }
function exportTmxAction() { $defaults = new Ps_AbstractViewModel(); $defaults->class_name = 'Ps_TranslationView'; $defaults->id = Ps_TranslationView::DEFAULT_ID; $view = Ps_AbstractViewLoader::getView(Ps_TranslationView::DEFAULT_ID, $defaults); // Extract every result from the view list($results, $null) = DAO_Translation::search($view->params, -1, 0, SearchFields_Translation::STRING_ID, true, false); // Build TMX outline $xml = simplexml_load_string('<?xml version="1.0" encoding="' . LANG_CHARSET_CODE . '"?>' . '<!DOCTYPE tmx SYSTEM "tmx14.dtd">' . '<tmx version="1.4">' . '<body></body>' . '</tmx>'); /* @var $xml SimpleXMLElement */ $namespaces = $xml->getNamespaces(true); $codes = array(); // Loop translated strings if (is_array($results)) { foreach ($results as $result) { $string_id = $result[SearchFields_Translation::STRING_ID]; $lang_code = $result[SearchFields_Translation::LANG_CODE]; $string_default = $result[SearchFields_Translation::STRING_DEFAULT]; $string_override = $result[SearchFields_Translation::STRING_OVERRIDE]; $codes[$lang_code] = 1; $string = !empty($string_override) ? $string_override : $string_default; // [TODO] Nest multiple <tuv> in a single <tu> parent $eTu =& $xml->body->addChild('tu'); /* @var $eTu SimpleXMLElement */ $eTu->addAttribute('tuid', $string_id); $eTuv =& $eTu->addChild('tuv'); /* @var $eTuv SimpleXMLElement */ $eTuv->addAttribute('xml:lang', $lang_code, 'http://www.w3.org/XML/1998/namespace'); $eSeg =& $eTuv->addChild('seg', htmlspecialchars($string)); /* @var $eSeg SimpleXMLElement */ } } $imp = new DOMImplementation(); // $dtd = $imp->createDocumentType('tmx', '', 'tmx14.dtd'); // $doc = $imp->createDocument("", "", $dtd); $doc = $imp->createDocument("", ""); $doc->encoding = LANG_CHARSET_CODE; $doc->formatOutput = true; $simplexml = dom_import_simplexml($xml); /* @var $dom DOMElement */ $simplexml = $doc->importNode($simplexml, true); $simplexml = $doc->appendChild($simplexml); $filename = "portsensor_lang_" . implode('_', array_keys($codes)) . ".xml"; header("Content-type: text/xml"); header("Content-Disposition: attachment; filename=\"{$filename}\""); echo $doc->saveXML(); }
function saveAlertPeekAction() { $translate = DevblocksPlatform::getTranslationService(); @($id = DevblocksPlatform::importGPC($_REQUEST['id'], 'integer', 0)); @($view_id = DevblocksPlatform::importGPC($_POST['view_id'], 'string')); @($active_worker = PortSensorApplication::getActiveWorker()); /*****************************/ @($name = DevblocksPlatform::importGPC($_POST['name'], 'string', '')); @($is_disabled = DevblocksPlatform::importGPC($_POST['is_disabled'], 'integer', 0)); @($worker_id = DevblocksPlatform::importGPC($_POST['worker_id'], 'integer', 0)); @($rules = DevblocksPlatform::importGPC($_POST['rules'], 'array', array())); @($do = DevblocksPlatform::importGPC($_POST['do'], 'array', array())); @($delete = DevblocksPlatform::importGPC($_POST['do_delete'], 'integer', 0)); if (!empty($id) && !empty($delete)) { DAO_Alert::delete($id); } else { if (empty($name)) { $name = $translate->_('Alert'); } $criterion = array(); $actions = array(); // Custom fields $custom_fields = DAO_CustomField::getAll(); $alert_criteria_exts = DevblocksPlatform::getExtensions('portsensor.alert.criteria', false); // Criteria if (is_array($rules)) { foreach ($rules as $rule) { $rule = DevblocksPlatform::strAlphaNumDash($rule); @($value = DevblocksPlatform::importGPC($_POST['value_' . $rule], 'string', '')); // [JAS]: Allow empty $value (null/blank checking) $criteria = array('value' => $value); // Any special rule handling switch ($rule) { case 'dayofweek': // days $days = DevblocksPlatform::importGPC($_REQUEST['value_dayofweek'], 'array', array()); if (in_array(0, $days)) { $criteria['sun'] = 'Sunday'; } if (in_array(1, $days)) { $criteria['mon'] = 'Monday'; } if (in_array(2, $days)) { $criteria['tue'] = 'Tuesday'; } if (in_array(3, $days)) { $criteria['wed'] = 'Wednesday'; } if (in_array(4, $days)) { $criteria['thu'] = 'Thursday'; } if (in_array(5, $days)) { $criteria['fri'] = 'Friday'; } if (in_array(6, $days)) { $criteria['sat'] = 'Saturday'; } unset($criteria['value']); break; case 'timeofday': $from = DevblocksPlatform::importGPC($_REQUEST['timeofday_from'], 'string', ''); $to = DevblocksPlatform::importGPC($_REQUEST['timeofday_to'], 'string', ''); $criteria['from'] = $from; $criteria['to'] = $to; unset($criteria['value']); break; case 'event': @($events = DevblocksPlatform::importGPC($_REQUEST['value_event'], 'array', array())); if (is_array($events)) { foreach ($events as $event) { $criteria[$event] = true; } } unset($criteria['value']); break; case 'alert_last_ran': @($from = DevblocksPlatform::importGPC($_REQUEST['value_alert_last_ran_from'], 'string', '')); @($to = DevblocksPlatform::importGPC($_REQUEST['value_alert_last_ran_to'], 'string', '')); $criteria['from'] = $from; $criteria['to'] = $to; unset($criteria['value']); break; case 'sensor_name': break; case 'sensor_fail_count': $oper = DevblocksPlatform::importGPC($_REQUEST['oper_sensor_fail_count'], 'string', '='); $criteria['oper'] = $oper; break; case 'sensor_type': @($types = DevblocksPlatform::importGPC($_REQUEST['value_sensor_types'], 'array', array())); if (is_array($types)) { foreach ($types as $type) { $criteria[$type] = true; } } unset($criteria['value']); break; default: // ignore invalids // [TODO] Very redundant // Custom fields if ("cf_" == substr($rule, 0, 3)) { $field_id = intval(substr($rule, 3)); if (!isset($custom_fields[$field_id])) { continue; } // [TODO] Operators switch ($custom_fields[$field_id]->type) { case 'S': // string // string case 'T': // clob // clob case 'U': // URL @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', 'regexp')); $criteria['oper'] = $oper; break; case 'D': // dropdown // dropdown case 'M': // multi-dropdown // multi-dropdown case 'X': // multi-checkbox // multi-checkbox case 'W': // worker @($in_array = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id], 'array', array())); $out_array = array(); // Hash key on the option for quick lookup later if (is_array($in_array)) { foreach ($in_array as $k => $v) { $out_array[$v] = $v; } } $criteria['value'] = $out_array; break; case 'E': // date @($from = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_from'], 'string', '0')); @($to = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_to'], 'string', 'now')); $criteria['from'] = $from; $criteria['to'] = $to; unset($criteria['value']); break; case 'N': // number @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', '=')); $criteria['oper'] = $oper; $criteria['value'] = intval($value); break; case 'C': // checkbox $criteria['value'] = intval($value); break; } } elseif (isset($alert_criteria_exts[$rule])) { // Extensions // Save custom criteria properties try { $crit_ext = $alert_criteria_exts[$rule]->createInstance(); /* @var $crit_ext Extension_AlertCriteria */ $criteria = $crit_ext->saveConfig(); } catch (Exception $e) { // print_r($e); } } else { continue; } break; } $criterion[$rule] = $criteria; } } $alert_action_exts = DevblocksPlatform::getExtensions('portsensor.alert.action', false); // Actions if (is_array($do)) { foreach ($do as $act) { $action = array(); switch ($act) { // Forward a copy to... case 'email': @($emails = DevblocksPlatform::importGPC($_REQUEST['do_email'], 'array', array())); if (!empty($emails)) { $action = array('to' => $emails); } break; // Watcher notification // Watcher notification case 'notify': //@$emails = DevblocksPlatform::importGPC($_REQUEST['do_email'],'array',array()); //if(!empty($emails)) { $action = array(); //} break; default: // ignore invalids // Custom fields if ("cf_" == substr($act, 0, 3)) { $field_id = intval(substr($act, 3)); if (!isset($custom_fields[$field_id])) { continue; } $action = array(); switch ($custom_fields[$field_id]->type) { case 'S': // string // string case 'T': // clob // clob case 'U': // URL // URL case 'D': // dropdown // dropdown case 'W': // worker $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', ''); $action['value'] = $value; break; case 'M': // multi-dropdown // multi-dropdown case 'X': // multi-checkbox $in_array = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'array', array()); $out_array = array(); // Hash key on the option for quick lookup later if (is_array($in_array)) { foreach ($in_array as $k => $v) { $out_array[$v] = $v; } } $action['value'] = $out_array; break; case 'E': // date $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', ''); $action['value'] = $value; break; case 'N': // number // number case 'C': // checkbox $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', ''); $action['value'] = intval($value); break; } } elseif (isset($alert_action_exts[$act])) { // Save custom action properties try { $action_ext = $alert_action_exts[$act]->createInstance(); $action = $action_ext->saveConfig(); } catch (Exception $e) { // print_r($e); } } else { continue; } break; } $actions[$act] = $action; } } $fields = array(DAO_Alert::NAME => $name, DAO_Alert::IS_DISABLED => $is_disabled, DAO_Alert::WORKER_ID => $worker_id, DAO_Alert::CRITERIA_JSON => json_encode($criterion), DAO_Alert::ACTIONS_JSON => json_encode($actions)); // Create if (empty($id)) { $fields[DAO_Alert::POS] = 0; $id = DAO_Alert::create($fields); // Update } else { DAO_Alert::update($id, $fields); } } if (!empty($view_id)) { $view = Ps_AbstractViewLoader::getView($view_id); $view->render(); } }