/** * Should handle execution of the task, taking as much (optional) parameters as needed * * The parameters should be optional and failing to provide them should be handled by * the task */ public function execute($lineNr = null, $fieldId = null, $fieldSub = null, $fieldCalc = null) { $batch = $this->getBatch(); $import = $batch->getVariable('import'); if (!(isset($import['trackId']) && $import['trackId'] && $fieldId)) { // Do nothing return; } $tracker = $this->loader->getTracker(); $trackEngine = $tracker->getTrackEngine($import['trackId']); $fieldCodes = $import['fieldCodes']; $fieldModel = $trackEngine->getFieldsMaintenanceModel(true, 'edit'); $roundOrders = $import['roundOrders']; $saveData['gtf_id_field'] = $fieldId; $saveData['sub'] = $fieldSub; $saveData['gtf_id_track'] = $import['trackId']; $calcFields = is_array($fieldCalc) ? $fieldCalc : explode('|', trim($fieldCalc, '|')); if (!$calcFields) { return; } foreach ($calcFields as $field) { if (isset($fieldCodes[$field]) && $fieldCodes[$field]) { $saveData['gtf_calculate_using'][] = $fieldCodes[$field]; } else { // Actually this code currently is PULSE specific if (\MUtil_String::startsWith($field, '{r')) { $roundOrder = substr($field, 2, -1); if (isset($roundOrders[$roundOrder]) && $roundOrders[$roundOrder]) { $saveData['gtf_calculate_using'][] = $roundOrders[$roundOrder]; } else { $saveData['gtf_calculate_using'][] = $field; } } } } \MUtil_Echo::track($saveData, $fieldId); $fieldModel->save($saveData); }
/** * Is the path a rooted path? * On Windows this does not require a drive letter, but one is allowed * * Check OS specific plus check for urls * * @param string $path * @return boolean */ public static function isRootPath($path) { if (!$path) { return false; } // Quick checkes first and then something just in case if ('\\' == $path[0] || '/' == $path[0] || \MUtil_String::startsWith($path, DIRECTORY_SEPARATOR)) { return true; } // One more check for windows if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return preg_match('#^[a-zA-Z]:\\\\#', $path); } // The check for uri's (frp, http, https return preg_match('#^[a-zA-Z]+://#', $path); }
/** * Returns an array of items for a page. * * @param integer $offset Page offset * @param integer $itemCountPerPage Number of items per page * @return array */ public function getItems($offset, $itemCountPerPage) { // Cast to integers, as $itemCountPerPage can be string sometimes and that would fail later checks $offset = (int) $offset; $itemCountPerPage = (int) $itemCountPerPage; if ($this->_lastOffset === $offset && $this->_lastItemCount === $itemCountPerPage && null !== $this->_lastItems) { return $this->_lastItems; } $this->_lastOffset = $offset; $this->_lastItemCount = $itemCountPerPage; // Optimization: by using the MySQL feature SQL_CALC_FOUND_ROWS // we can get the count and the results in a single query. $db = $this->_select->getAdapter(); if (null === $this->_count && $db instanceof \Zend_Db_Adapter_Mysqli) { $this->_select->limit($itemCountPerPage, $offset); $sql = $this->_select->__toString(); if (\MUtil_String::startsWith($sql, 'select ', true)) { $sql = 'SELECT SQL_CALC_FOUND_ROWS ' . substr($sql, 7); } $this->_lastItems = $db->fetchAll($sql); $this->_count = $db->fetchOne('SELECT FOUND_ROWS()'); } else { $this->_lastItems = $this->_selectAdapter->getItems($offset, $itemCountPerPage); } if (is_array($this->_lastItems)) { if (isset($this->_model->prefetchIterator) && $this->_model->prefetchIterator) { $this->_lastItems = new \ArrayIterator($this->_lastItems); } $this->_lastItems = $this->_model->processAfterLoad($this->_lastItems, false, false); } return $this->_lastItems; }
/** * Returns true if and only if $value meets the validation requirements * * If $value fails validation, then this method returns false, and * getMessages() will return an array of messages that explain why the * validation failed. * * @param mixed $value * @param array $context * @return boolean * @throws \Zend_Validate_Exception If validation of $value is impossible */ public function isValid($value, $context = array()) { $this->_setValue($value); foreach ($context as $field => $val) { if (\MUtil_String::startsWith($field, 'survey__')) { $sid = intval(substr($field, 8)); if ($sid !== $this->_surveyId && $value == $val) { $this->_error(self::ERROR_RECORD_FOUND); return false; } $this->_tested[] = $sid; } } $result = $this->_query($value); if ($result) { $this->_error(self::ERROR_RECORD_FOUND); return false; } return true; }
/** * Get the structural patches for the given patch level * @param int $patchLevel * @return array */ public function getStructuralPatches($patchLevel) { $patches = $this->db->fetchAll("SELECT * FROM gems__patches WHERE gpa_level = ? ORDER BY gpa_location, gpa_name, gpa_order", $patchLevel); foreach ($patches as $patchId => $patch) { if (\MUtil_String::startsWith(trim($patch['gpa_sql']), 'INSERT', true) || \MUtil_String::startsWith(trim($patch['gpa_sql']), 'UPDATE', true) || \MUtil_String::startsWith(trim($patch['gpa_sql']), 'DELETE', true)) { unset($patches[$patchId]); } } return $patches; }
/** * Compile a less file * * @param \Zend_View $view * @param string $href The less file * @param boolean $always Always compile * @return boolean True when changed */ public function compile(\Zend_View $view, $href, $always = false) { if (\MUtil_String::startsWith($href, 'http', true)) { // When a local url, strip the serverUrl and basepath $base = $view->serverUrl() . $view->baseUrl(); if (\MUtil_String::startsWith($href, $base, true)) { // Only strip when urls match $href = substr($href, strlen($base)); } } // Add full path to the webdir $inFile = $this->_webroot . '/' . $href; $outFile = substr($inFile, 0, -strlen(pathinfo($inFile, PATHINFO_EXTENSION))) . 'css'; // Try compiling try { // \MUtil_Echo::track($inFile, $outFile); $lessc = new lessc(); $lessc->registerFunction('base64encode', array($this, 'base64encode')); if ($always || array_key_exists('compilecss', \Zend_Controller_Front::getInstance()->getRequest()->getParams())) { $result = (bool) $lessc->compileFile($inFile, $outFile); } else { $result = $lessc->checkedCompile($inFile, $outFile); } } catch (\Exception $exc) { // If we have an error, present it if not in production if (APPLICATION_ENV !== 'production' || APPLICATION_ENV !== 'acceptance') { \MUtil_Echo::pre($exc->getMessage()); } $result = null; } return $result; }
/** * Converting the field value when saving to a respondent track * * @param array $currentValue The current value * @param array $fieldData The other values loaded so far * @return mixed the new value */ public function onFieldDataSave($currentValue, array $fieldData) { if (null === $currentValue || $currentValue instanceof \Zend_Db_Expr || \MUtil_String::startsWith($currentValue, 'current_', true)) { return $currentValue; } $saveFormat = $this->getStorageFormat(); if ($currentValue instanceof \Zend_Date) { return $currentValue->toString($saveFormat); } else { $displayFormat = $this->getDateFormat(); $saveDate = \MUtil_Date::ifDate($currentValue, array($displayFormat, $saveFormat, \Gems_Tracker::DB_DATETIME_FORMAT)); if ($saveDate instanceof \Zend_Date) { return $saveDate->toString($saveFormat); } } return (string) $currentValue; }
/** * A ModelAbstract->setOnSave() function that returns the input * date as a valid date. * * @see \MUtil_Model_ModelAbstract * * @param mixed $value The value being saved * @param boolean $isNew True when a new item is being saved * @param string $name The name of the current field * @param array $context Optional, the other values being saved * @return \Zend_Date */ public function formatSaveDate($value, $isNew = false, $name = null, array $context = array()) { if ($name && !(null === $value || $value instanceof \Zend_Db_Expr || \MUtil_String::startsWith($value, 'current_', true))) { $saveFormat = $this->getWithDefault($name, 'storageFormat', \Zend_Date::ISO_8601); if ($value instanceof \Zend_Date) { return $value->toString($saveFormat); } else { $displayFormat = $this->get($name, 'dateFormat'); try { return \MUtil_Date::format($value, $saveFormat, $displayFormat); } catch (\Zend_Exception $e) { if (\Zend_Date::isDate($value, $saveFormat)) { return $value; } throw $e; } } } return $value; }
/** * Haystack starts with needle */ public function testStartsWithTrue() { $result = MUtil_String::startsWith('abcdef', 'abc'); $this->assertEquals($result, true); }
/** * Converting the field value when saving to a respondent track * * @param array $currentValue The current value * @param array $fieldData The other values loaded so far * @return mixed the new value */ public function onFieldDataSave($currentValue, array $fieldData) { if (null === $currentValue || $currentValue instanceof \Zend_Db_Expr || \MUtil_String::startsWith($currentValue, 'current_', true)) { return $currentValue; } $saveFormat = $this->getStorageFormat(); if ($currentValue instanceof \Zend_Date) { return $currentValue->toString($saveFormat); } else { $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat'); $returnDate = \MUtil_Date::format($currentValue, $saveFormat, $displayFormat); if (is_null($returnDate)) { if (\Zend_Date::isDate($currentValue, $saveFormat)) { return $currentValue; } } return $returnDate; } return (string) $currentValue; }