/**
  * 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;
    }
 /**
  * Save workflow out of the browser
  * 
  * @param sfWebRequest $request
  * @return <type>
  */
 public function executeSaveWorkflow(sfWebRequest $request) {
     sfLoader::loadHelpers('Url');
     $data = $request->getPostParameters();
     $workflowSaveObj = new SaveWorkflow();
     if($data['workfloweditAcceptWorkflow_decission'] == 1) { // user accepted Workflow
         // workflow contains fields to write, not e.g. not only file-fields
         if(isset($data['field'])) {
             foreach($data['field'] as $field) {
                 switch ($field['type']) {
                 case 'TEXTFIELD':
                     WorkflowSlotFieldTextfieldTable::instance()->updateTextfieldByWorkflowFieldId($field['field_id'],$field['value']);
                     break;
                 case 'CHECKBOX':
                     $value = $field['value'] == 'false' ? 0 : 1;
                     WorkflowSlotFieldCheckboxTable::instance()->updateCheckboxByWorkflowFieldId($field['field_id'],$value);
                     break;
                 case 'NUMBER':
                     WorkflowSlotFieldNumberTable::instance()->updateNumberByWorkflowFieldId($field['field_id'],$field['value']);
                     break;
                 case 'DATE':
                     WorkflowSlotFieldDateTable::instance()->updateDateByWorkflowFieldId($field['field_id'],$field['value']);
                     break;
                 case 'TEXTAREA':
                     WorkflowSlotFieldTextareaTable::instance()->updateTextareaByWorkflowFieldId($field['field_id'],$field['value']);
                     break;
                 case 'RADIOGROUP':
                     $items = $field['item'];
                     foreach($items as $item) {
                         $value = $item['value'] == 'false' ? 0 : 1;
                         WorkflowSlotFieldRadiogroupTable::instance()->updateRadiogroupById($item['id'],$value);
                     }
                     break;
                 case 'CHECKBOXGROUP':
                     $items = $field['item'];
                     foreach($items as $item) {
                         $value = $item['value'] == 'false' ? 0 : 1;
                         WorkflowSlotFieldCheckboxgroupTable::instance()->updateCheckboxgroupById($item['id'],$value);
                     }
                     break;
                 case 'COMBOBOX':
                     $items = $field['item'];
                     foreach($items as $item) {
                         $value = $item['value'] == 'false' ? 0 : 1;
                         WorkflowSlotFieldComboboxTable::instance()->updateComboboxById($item['id'],$value);
                     }
                     break;
                 case 'FILE':
                     break;
                 }
             }
         }
         // calculate next station
         $context = sfContext::getInstance();
         $context->getConfiguration()->loadHelpers('Partial', 'I18N', 'Url', 'Date', 'CalculateDate', 'ColorBuilder', 'Icon', 'EndAction');
         $slots = $data['slot'];
         $workflowSaveObj->setContext($context);
         $workflowSaveObj->setServerUrl(str_replace('/layout', '', url_for('layout/index',true)));
         $workflowSaveObj->getNextStation($slots,$this->getUser()->getAttribute('id'),$request->getParameter('versionid'));
     }
     else { // user denies workflow
         $workflowSaveObj->denyWorkflow($data, $request->getParameter('workflowid'), $this->getUser()->getAttribute('id'), $request->getParameter('versionid'));
     }
     $this->renderText('{success:true}');
     return sfView::NONE;
 }
    /**
     * 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 '';
                    }
                }
            }
        }
    }