/**
     * Function loads an attachment, and switches between file as attachment and file as field.
     * The files are stored in web/upload dir containing WorkflowID/WorkflowversionID/md5(date_filename)
     * @param sfWebRequest $request
     * @return <type>
     */
    public function executeShowAttachment(sfWebRequest $request) {
        $versionid = $request->getParameter('versionid');
        $workflowid = $request->getParameter('workflowid');
        $attachmentid = $request->getParameter('attachmentid');
        $file = $request->getParameter('file');

        if ($file == 1) {
            $attachment = WorkflowSlotFieldFileTable::instance()->geFileById($attachmentid)->toArray(); // file from field
        }
        else {
            $attachment = WorkflowVersionAttachmentTable::instance()->getAttachmentsById($attachmentid)->toArray(); // file from attachment table
        }
        $filepath = sfConfig::get('sf_upload_dir') . '/' . $workflowid . '/' . $versionid . '/' . $attachment[0]['hashname'];
        $file = new File();

        $filecontent = $file->getFileContent($filepath); // open file and get content
        $contenttyoe = $file->getContenttype($attachment[0]['hashname']);

        $response = $this->getResponse();
        $response->clearHttpHeaders();

        $response->setHttpHeader('Content-Type', 'application/octet-stream'); // set content type of response
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=' . $attachment[0]['filename']);
        $response->setHttpHeader('Content-Length', @filesize($filepath)); // add filesize
        $response->sendHttpHeaders(); // send the headers before the file
        $response->setContent($filecontent); // set file string
        return sfView::NONE;
    }
 /**
  * Add all fields with its values to the slot. Value depends on the flag if old values are used or not
  * @param <type> $type
  * @param <type> $field_id
  * @param <type> $workflowslotfield_id
  * @return <type>
  */
 public function getItems($type, $field_id, $workflowslotfield_id) {
     $result = array();
     $a = 0;
     switch($type) {
         case 'TEXTFIELD':
             if($this->newValue == 0) {
                 $data = FieldTextfieldTable::instance()->getTextfieldByFieldId($field_id)->toArray();
                 $result[0]['value'] = $data[0]['defaultvalue'];
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldTextfieldTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 $result[0]['value'] = $data[0]['value'];
                 return $result;
             }
             break;
         case 'CHECKBOX':
             if($this->newValue == 0) {
                 $result[0]['value'] = 0;
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldCheckboxTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 $result[0]['value'] = $data[0]['value'];
                 return $result;
             }
             break;
         case 'NUMBER':
             if($this->newValue == 0) {
                 $data = FieldNumberTable::instance()->getNumberByFieldId($field_id)->toArray();
                 $result[0]['value'] = $data[0]['defaultvalue'];
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldNumberTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 $result[0]['value'] = $data[0]['value'];
                 return $result;
             }
             break;
         case 'DATE':
             if($this->newValue == 0) {
                 $data = FieldDateTable::instance()->getDateByFieldId($field_id)->toArray();
                 $result[0]['value'] = $data[0]['defaultvalue'];
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldDateTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 $result[0]['value'] = $data[0]['value'];
                 return $result;
             }
             break;
         case 'TEXTAREA':
             if($this->newValue == 0) {
                 $data = FieldTextareaTable::instance()->getTextareaByFieldId($field_id)->toArray();
                 $result[0]['value'] = $data[0]['content'];
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldTextareaTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 $result[0]['value'] = $data[0]['value'];
                 return $result;
             }
             break;
         case 'RADIOGROUP':
             if($this->newValue == 0) {
                 $data = FieldRadiogroupTable::instance()->findRadiogroupByFieldId($field_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['isactive'];
                     $result[$a]['fieldradiogroup_id'] = $item['id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldRadiogroupTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['value'];
                     $result[$a]['fieldradiogroup_id'] = $item['fieldradiogroup_id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             break;
         case 'CHECKBOXGROUP':
             if($this->newValue == 0) {
                 $data = FieldCheckboxgroupTable::instance()->findCheckboxgroupByFieldId($field_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['isactive'];
                     $result[$a]['fieldradiogroup_id'] = $item['id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldCheckboxgroupTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['value'];
                     $result[$a]['fieldradiogroup_id'] = $item['fieldcheckboxgroup_id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             break;
         case 'COMBOBOX':
             if($this->newValue == 0) {
                 $data = FieldComboboxTable::instance()->findComboboxByFieldId($field_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['isactive'];
                     $result[$a]['fieldradiogroup_id'] = $item['id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             else {
                 $data = WorkflowSlotFieldComboboxTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
                 foreach($data as $item) {
                     $result[$a]['value'] = $item['value'];
                     $result[$a]['fieldradiogroup_id'] = $item['fieldcombobox_id'];
                     $result[$a++]['position'] = $item['position'];
                 }
                 return $result;
             }
             break;
         case 'FILE':
             $data = WorkflowSlotFieldFileTable::instance()->getAllItemsByWorkflowFieldId($workflowslotfield_id)->toArray();
             return $data;
             break;
     }
 }
    public function getFieldItems(WorkflowSlotField $field, $type, sfContext $context, $versionid) {
        $result = array();
        $a = 0;
        switch ($type) {
            case 'TEXTFIELD':
                $items = WorkflowSlotFieldTextfieldTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $fieldData = FieldTextfieldTable::instance()->getTextfieldByFieldId($field->getFieldId())->toArray();
                $replaceObj = new ReplaceTags($versionid, $items[0]['value'], $this->culture, $context);
                $value = $replaceObj->getText();
                $result['value'] = $value;
                $result['regex'] = $fieldData[0]['regex'];
                $result['id'] = $items[0]['id'];
                break;
            case 'CHECKBOX':
                $items = WorkflowSlotFieldCheckboxTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $result['value'] = $items[0]['value'];
                $result['id'] = $items[0]['id'];
                break;
            case 'NUMBER':
                $items = WorkflowSlotFieldNumberTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $fieldData = FieldNumberTable::instance()->getNumberByFieldId($field->getFieldId())->toArray();
                if($fieldData[0]['comboboxvalue'] != 'EMPTY') {
                    $result['emptytext'] = $context->getI18N()->__($fieldData[0]['comboboxvalue'] ,null,'field');
                }
                else {
                    $result['emptytext'] = $fieldData[0]['regex'];
                }
                $result['value'] = $items[0]['value'];
                $result['regex'] = $fieldData[0]['regex'];
                $result['id'] = $items[0]['id'];
                break;
            case 'DATE':
                $items = WorkflowSlotFieldDateTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $format = FieldDateTable::instance()->getDateByFieldId($field->getFieldId())->toArray();
                $result['value'] = $items[0]['value'];
                $result['dateformat'] = $format[0]['dateformat'];
                $result['regex'] = $format[0]['regex'];
                $result['id'] = $items[0]['id'];
                break;
            case 'TEXTAREA':
                $items = WorkflowSlotFieldTextareaTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $textarea = FieldTextareaTable::instance()->getTextareaById($field->getFieldId())->toArray();
                $result['value'] = $items[0]['value'];
                $result['contenttype'] = $textarea[0]['contenttype'];
                $result['id'] = $items[0]['id'];
                break;
            case 'RADIOGROUP':
                $items = WorkflowSlotFieldRadiogroupTable::instance()->getAllItemsByWorkflowFieldId($field->getId());
                foreach($items as $item) {
                    $name = FieldRadiogroupTable::instance()->getRadiogroupItemById($item->getFieldradiogroupId())->toArray();
                    $result[$a]['value'] = $item->getValue();
                    $result[$a]['id'] = $item->getId();
                    $result[$a++]['name'] = $name[0]['value'];
                }
                break;
            case 'CHECKBOXGROUP':
                $items = WorkflowSlotFieldCheckboxgroupTable::instance()->getAllItemsByWorkflowFieldId($field->getId());
                foreach($items as $item) {
                    $name = FieldCheckboxgroupTable::instance()->getCheckboxgroupItemById($item->getFieldcheckboxgroupId())->toArray();
                    $result[$a]['value'] = $item->getValue();
                    $result[$a]['id'] = $item->getId();
                    $result[$a++]['name'] = $name[0]['value'];
                }
                break;
            case 'COMBOBOX':
                $items = WorkflowSlotFieldComboboxTable::instance()->getAllItemsByWorkflowFieldId($field->getId());
                foreach($items as $item) {
                    $name = FieldComboboxTable::instance()->getComboboxItemById($item->getFieldcomboboxId())->toArray();
                    $result[$a]['value'] = $item->getValue();
                    $result[$a]['id'] = $item->getId();
                    $result[$a++]['name'] = $name[0]['value'];
                }
                break;
            case 'FILE':

                $file = WorkflowSlotFieldFileTable::instance()->getAllItemsByWorkflowFieldId($field->getId())->toArray();
                $workflowtemplate = WorkflowVersionTable::instance()->getWorkflowVersionById($versionid)->toArray();
                $result['filepath'] = sfConfig::get('sf_upload_dir') . '/' . $workflowtemplate[0]['workflowtemplate_id'] . '/' . $versionid . '/' . $file[0]['hashname'] ;
                $result['hashname'] = $file[0]['hashname'];
                $result['filename'] = $file[0]['filename'];
                $url = $this->serverUrl . '/file/ShowAttachment';
                $plainUrl = $this->serverUrl . '/file/ShowAttachment';
                $url .= '/workflowid/' .  $workflowtemplate[0]['workflowtemplate_id'] . '/versionid/' . $versionid. '/attachmentid/' . $file[0]['id'] . '/file/1';
                $plainUrl .= '/workflowid/' .  $workflowtemplate[0]['workflowtemplate_id'] . '/versionid/' . $versionid. '/attachmentid/' . $file[0]['id'] . '/file/1';
                $result['plainurl'] = $plainUrl;
                $result['url'] = $url;
                $result['link'] = '<a href="'.$url.'" target="_blank">'.$result['filename'].'</a>';
                break;
        }
        return $result;
    }
    /**
     * Get userdefined field
     *
     * @param String  $type, userdefined1, userdefined2
     */
    public function getFields($type, $versionId) {
        $view = $this->user->getAttribute('userWorkflowSettings');
        $result = '';
        foreach($view as $singleView) {
            if($singleView['store'] == $type AND $singleView['fieldid'] > -1) {
                $wfItem = WorkflowVersionTable::instance()->getFieldByWorkflowversionIdAndFieldId($singleView['fieldid'], $versionId)->toArray();
                if(empty($wfItem) == true) {
                    return '';
                }
                else {
                    $slots = WorkflowSlotTable::instance()->getFieldBySlotIdAndFieldId($singleView['fieldid'], $versionId)->toArray();
                    $fields = WorkflowSlotFieldTable::instance()->getWorkflowSlotFieldBySlotIdAndFieldId($slots[0]['id'],$singleView['fieldid'])->toArray();
                    $theType = FieldTable::instance()->getFieldById($fields[0]['field_id'])->toArray();
                    if(!empty($theType)) {
                        switch ($theType[0]['type']) {
                            case 'TEXTFIELD':
                                $value = WorkflowSlotFieldTextfieldTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'];
                                return $result;
                                break;
                            case 'CHECKBOX':
                                $value = WorkflowSlotFieldCheckboxTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'] == 1 ? $this->context->getI18N()->__('Yes' ,null,'workflowmanagement') : $this->context->getI18N()->__('No' ,null,'workflowmanagement');
                                return $result;
                                break;
                            case 'NUMBER':
                                $value = WorkflowSlotFieldNumberTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'];
                                return $result;
                                break;
                            case 'DATE':
                                $value = WorkflowSlotFieldDateTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'];
                                return $result;
                                break;
                            case 'TEXTAREA':
                                $value = WorkflowSlotFieldTextareaTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'];
                                return $result;
                                break;
                            case 'RADIOGROUP':
                                $value = WorkflowSlotFieldRadiogroupTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'] == 1 ? $this->context->getI18N()->__('Yes' ,null,'workflowmanagement') : $this->context->getI18N()->__('No' ,null,'workflowmanagement');
                                return $result;
                                break;
                            case 'CHECKBOXGROUP':
                                $value = WorkflowSlotFieldCheckboxgroupTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'] == 1 ? $this->context->getI18N()->__('Yes' ,null,'workflowmanagement') : $this->context->getI18N()->__('No' ,null,'workflowmanagement');
                                return $result;
                                break;
                            case 'COMBOBOX':
                                $value = WorkflowSlotFieldComboboxTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $result = $value[0]['value'] == 1 ? $this->context->getI18N()->__('Yes' ,null,'workflowmanagement') : $this->context->getI18N()->__('No' ,null,'workflowmanagement');
                                return $result;
                                break;
                            case 'FILE':
                                $value = WorkflowSlotFieldFileTable::instance()->getAllItemsByWorkflowFieldId($fields[0]['id'])->toArray();
                                $url = (url_for('layout/index',true));
                                $url = str_replace('/layout', '', $url);
                                $result['filepath'] = $url . '/file/showAttachment/workflowid/' . $wfItem[0]['workflowtemplate_id'] . '/versionid/' . $wfItem[0]['id'] . '/attachmentid/' . $value[0]['id'] . '/file/1';
                                $fileUrl = '<a href="'.$result['filepath'].'">'.$value[0]['filename'].'</a>';
                                return $fileUrl;
                                break;

                        }
                    }
                    else {
                        return '';
                    }
                }
            }
        }
    }