/**
  * @Saving
  */
 public static function saving($event, Field $field)
 {
     $formmaker = App::module('bixie/formmaker');
     if (!($type = $formmaker->getFieldType($field->type))) {
         throw new Exception(__('Field type not found.'));
     }
     foreach (['multiple', 'required'] as $key) {
         if ($type[$key] != -1) {
             //check fixed value
             if ($type[$key] != $field->get($key)) {
                 throw new Exception(__('Invalid value for ' . $key . ' option.'));
             }
         }
     }
     //slug
     $i = 2;
     $id = $field->id;
     if (!$field->slug) {
         $field->slug = $field->label;
     }
     while (self::where(['slug = ?', 'form_id = ?'], [$field->slug, $field->form_id])->where(function ($query) use($id) {
         if ($id) {
             $query->where('id <> ?', [$id]);
         }
     })->first()) {
         $field->slug = preg_replace('/-\\d+$/', '', $field->slug) . '-' . $i++;
     }
     if (!$field->id) {
         $field->priority = 1 + self::getConnection()->fetchColumn('SELECT MAX(priority) FROM @formmaker_field');
     }
 }
 /**
  * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
  * @Request({"id": "int"}, csrf=true)
  */
 public function deleteAction($id)
 {
     if ($form = Form::find($id)) {
         foreach (Field::where(['form_id = :id'], [':id' => $id])->get() as $field) {
             $field->delete();
         }
         $form->delete();
     }
     return ['message' => 'success'];
 }
Example #3
0
 public function getFieldsubmissions()
 {
     if (!isset($this->fieldsubmissions)) {
         $fields = $this->form->getFields();
         foreach ($this->data as $submissionvalue) {
             if (isset($fields[$submissionvalue['field_id']])) {
                 $field = $fields[$submissionvalue['field_id']];
             } else {
                 //field might be deleted from form
                 $field = Field::create();
                 $field->setFieldType($submissionvalue['type']);
             }
             $this->fieldsubmissions[$field->slug] = (new Fieldsubmission($field, $submissionvalue))->toFormattedArray();
         }
     }
     return $this->fieldsubmissions;
 }
Example #4
0
 /**
  * @return array|Field[]
  */
 public function getFields()
 {
     if (!isset($this->fields)) {
         $fields = Field::where(['form_id' => $this->id])->orderBy('priority', 'ASC')->get();
     } else {
         $fields = $this->fields;
     }
     //make sure type exists
     $formmaker = App::module('bixie/formmaker');
     $this->fields = [];
     foreach ($fields as $field) {
         if ($formmaker->getFieldType($field->type)) {
             $this->fields[$field->id] = $field;
         }
     }
     return $this->fields;
 }
 /**
  * @Route("/csv", methods="GET")
  * @Request({"options": "array"}, csrf=true)
  */
 public function indexCsvAction($options = [])
 {
     $count = 0;
     $forms = [];
     $fields = [];
     $form = ['id' => 0];
     $options = new ArrObject($options);
     if ($form_id = $options->get('form_id', 0)) {
         //get forminfo
         $form = Form::find($form_id);
         $fields = Field::where(['form_id = ?'], [$form_id])->get();
         $options->set('field_ids', array_keys($fields));
         //count exported submissions
         $query = Submission::query();
         $query->where(['form_id = ?'], [$form_id])->whereIn('status', $options->get('status', []));
         //input cleaned?
         $count = $query->count();
     } else {
         $forms = array_values(Form::findAll());
     }
     //force int
     $options->set('status', array_map(function ($id) {
         return (int) $id;
     }, $options->get('status', [])));
     $options->set('form_id', (int) $form_id);
     return ['options' => $options->toArray(), 'forms' => $forms, 'formitem' => $form, 'fields' => array_values($fields), 'count' => $count];
 }
 /**
  * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
  * @Request({"id": "int"}, csrf=true)
  */
 public function deleteAction($id)
 {
     if ($field = Field::find($id)) {
         $field->delete();
     }
     return ['message' => 'success'];
 }