/** * Test text converter */ public function testConvertPart3() { $this->markTestIncomplete('do not use helpdesk'); $model = new Helpdesk_Models_Helpdesk(); $order = Phprojekt_ModelInformation_Default::ORDERING_FORM; $fieldDefinition = $model->getInformation()->getFieldDefinition($order); $model->title = 'test'; $model->projectId = 1; $model->ownerId = 1; $model->attachments = '3bc3369dd33d3ab9c03bd76262cff633|LICENSE'; $model->status = 3; $model->author = 2; foreach ($fieldDefinition as $info) { // Upload if ($info['key'] == 'attachments') { $value = Phprojekt_Converter_Text::convert($model, $info); $this->assertEquals('LICENSE', $value); $model->attachments = '6d54feaa915a99cce5850d7812ade10e|LICENSE|' . '|0620dbcea94f89e8154682d21bc327b0|install.log'; $value = Phprojekt_Converter_Text::convert($model, $info); $this->assertEquals('LICENSE, install.log', $value); } // Display if ($info['key'] == 'author') { $value = Phprojekt_Converter_Text::convert($model, $info); $this->assertEquals('Solt, Gustavo', $value); $model->author = 100; $value = Phprojekt_Converter_Text::convert($model, $info); $this->assertEquals('100', $value); } } }
/** * Convert a model or a model information into a CSV file. * * @param Phprojekt_Interface_Model|array $models The model to convert. * @param integer $order A Phprojekt_ModelInformation_Default::ORDERING_* * const that defines the ordering for the convert. * @param boolean $exportHeader Determine if the header needs to be exported. * * @return string Data in CSV format. */ private static function _convertModel($models, $order = Phprojekt_ModelInformation_Default::ORDERING_DEFAULT, $exportHeader = true) { $datas = array(); $data = array(); $model = current((array) $models); $information = $model->getInformation($order); // Csv file header if ($exportHeader) { $metadata = $information->getFieldDefinition($order); if (is_array($metadata)) { foreach ($metadata as $oneCol) { $data[] = $oneCol['label']; } } $datas[] = $data; } foreach ($models as $cmodel) { $data = array(); foreach ($information->getFieldDefinition($order) as $field) { $key = $field['key']; $value = Phprojekt_Converter_Text::convert($cmodel, $field); $data[] = $value; } $datas[] = $data; } return self::_writeFile($datas); }
/** * Returns the fields part of the Notification body. * * @param Zend_Locale $lang Locale for use in translations. * * @return array Array with 'field', 'label' and 'value'. */ public function getBodyFields($lang) { $order = Phprojekt_ModelInformation_Default::ORDERING_FORM; $fieldDefinition = $this->_model->getInformation()->getFieldDefinition($order); $bodyFields = array(); foreach ($fieldDefinition as $key => $field) { switch ($field['type']) { case 'selectbox': case 'multipleselectbox': // Search the value foreach ($field['range'] as $range) { if ($range['id'] == $this->_model->{$field}['key']) { $value = isset($range['originalName']) ? Phprojekt::getInstance()->translate($range['originalName'], $lang) : $range['name']; break; } } break; case 'display': // Search if there is an Id value that should be translated into a descriptive String $value = null; foreach ($field['range'] as $range) { if (is_array($range)) { if ($range['id'] == $this->_model->{$field}['key']) { $value = isset($range['originalName']) ? Phprojekt::getInstance()->translate($range['originalName'], $lang) : $range['name']; break 2; } } } if (null === $value) { $value = $this->_model->{$field}['key']; } break; default: $value = Phprojekt_Converter_Text::convert($this->_model, $field); break; } $bodyFields[] = array('field' => $field['key'], 'label' => Phprojekt::getInstance()->translate($field['originalLabel'], $lang), 'value' => $value); } return $bodyFields; }
/** * Converts a single value for use in notifications. * * @param array $field The field definition for this value * @param mixed $value The value to convert. * @param Zend_Locale $lang The language to use for localization. * * @return string A string representing $value. */ private function _convert($field, $value, $lang) { switch ($field['type']) { case 'selectbox': case 'multipleselectbox': // Search the value foreach ($field['range'] as $range) { if ($range['id'] == $value) { return isset($range['originalName']) ? Phprojekt::getInstance()->translate($range['originalName'], $lang) : $range['name']; } } //TODO: What if it's not in range? break; case 'display': // Search if there is an Id value that should be translated into a descriptive String foreach ($field['range'] as $range) { if (is_array($range)) { if ($range['id'] == $this->_model->{$field}['key']) { return isset($range['originalName']) ? Phprojekt::getInstance()->translate($range['originalName'], $lang) : $range['name']; } } } return $this->_model->{$field}['key']; default: return Phprojekt_Converter_Text::convert($this->_model, $field); } }
/** * Index a object. * * First delete all the entries for this object for delete the unused strings. * Then get all the fields and values to index. * Then index each of one. * * @param Phprojekt_Item_Abstract $object The item object. * * @return void */ public function indexObjectItem($object) { $words = array(); $moduleId = Phprojekt_Module::getId($object->getModelName()); $itemId = $object->id; $wordsId = $this->_wordModule->deleteWords($moduleId, $itemId); $this->_words->decreaseWords($wordsId); $data = $this->_getObjectDataToIndex($object); $this->_display->saveDisplay($object, $moduleId, $itemId); foreach ($data as $key => $value) { $type = $object->getInformation()->getType($key); if (null !== $type) { if ($type == 'upload') { $field = array('type' => $type, 'key' => $key); $files = Phprojekt_Converter_Text::convert($object, $field); $value = $this->_files->getWordsFromFile($files); } if ($type != 'hidden') { $words[] = $value; } } } $wordsId = $this->_words->indexWords(implode(" ", $words)); $this->_wordModule->indexWords($moduleId, $itemId, $wordsId); }
/** * Return the data array with all the changes for a item ID. * * The data is sorted by date and have all the values stored in the database. * The data result is for use with a template that correct the values for the user. * * @param Phprojekt_Item_Abstract $object The item object. * @param integer $itemId The item ID. * @param integer $moduleId The ID of the module (optional). * @param date $startDate Start date of the history list (optional). * @param date $endDate End date of the history list (optional). * @param integer $userId User filter (optional). * * @return array Array with 'userId', 'moduleId', 'itemId', 'field', 'label', * 'oldValue', 'newValue', 'action' and 'datetime'. */ public function getHistoryData($object, $itemId, $moduleId = null, $startDate = null, $endDate = null, $userId = null) { if (!isset($moduleId)) { $moduleId = Phprojekt_Module::getId($object->getModelName()); } if (null === $object) { $moduleName = Phprojekt_Module::getModuleName($moduleId); $object = Phprojekt_Loader::getModel($moduleName, $moduleName); } $order = Phprojekt_ModelInformation_Default::ORDERING_FORM; $fieldDefinition = $object->getInformation()->getFieldDefinition($order); $where = sprintf('module_id = %d AND item_id = %d', (int) $moduleId, (int) $itemId); if (!empty($startDate)) { $where .= $this->getAdapter()->quoteInto(' AND datetime >= ?', $startDate); } if (!empty($endDate)) { $where .= $this->getAdapter()->quoteInto(' AND datetime <= ?', $endDate); } if (!empty($userId)) { $where .= sprintf(' AND user_id = %d', (int) $userId); } $result = array(); $fields = array(); foreach ($fieldDefinition as $field) { $fields[$field['key']] = $field; } foreach ($this->fetchAll($where, 'datetime DESC') as $row) { if (isset($fields[$row->field])) { $oldField = $fields[$row->field]; $oldField['key'] = 'oldValue'; $oldValue = Phprojekt_Converter_Text::convert($row, $oldField); $newField = $fields[$row->field]; $newField['key'] = 'newValue'; $newValue = Phprojekt_Converter_Text::convert($row, $newField); $label = $fields[$row->field]['label']; } else { $oldValue = $row->oldValue; $newValue = $row->newValue; $label = $row->field; } $oldValue = $this->_convertDateTimes($oldValue, $fields[$row->field]['type'], 'utcToUser'); $newValue = $this->_convertDateTimes($newValue, $fields[$row->field]['type'], 'utcToUser'); if ($oldValue != $newValue) { $result[] = array('userId' => (int) $row->userId, 'moduleId' => (int) $row->moduleId, 'itemId' => (int) $row->itemId, 'field' => $row->field, 'label' => $label, 'oldValue' => $oldValue, 'newValue' => $newValue, 'action' => $row->action, 'datetime' => $this->_convertDateTimes($row->datetime, 'datetime', 'utcToUser')); } } return $result; }