/**
  * {@inheritdoc}
  */
 public function main(App $app)
 {
     $app['field'] = function ($app) {
         if ($id = $app['request']->attributes->get('_field') and $field = Field::find($id)) {
             return $field;
         }
         return new Field();
     };
 }
 /**
  * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
  * @Request({"id": "int"}, csrf=true)
  */
 public function deleteAction($id)
 {
     if ($field = Field::find($id)) {
         foreach (Profilevalue::where(['field_id = :id'], [':id' => $id])->get() as $profilevalue) {
             $profilevalue->delete();
         }
         $field->delete();
     }
     return ['message' => 'success'];
 }
 /**
  * @Route("/ajax", methods="POST")
  * @Request({"field_id": "int", "action": "string"})
  */
 public function ajaxAction($field_id, $action)
 {
     if (!($field = Field::find($field_id))) {
         App::abort(400, __('Field not found.'));
     }
     $fieldValue = Profilevalue::create()->setField($field);
     $fieldType = $fieldValue->getFieldType();
     if (method_exists($fieldType, $action)) {
         return call_user_func([$fieldType, $action], $fieldValue);
     }
     return 'No response';
 }
 /**
  * @Route("/edit")
  * @Request({"id"})
  * @Access("site: manage site", admin=true)
  */
 public function editAction($id = '')
 {
     $userprofile = App::module('bixie/userprofile');
     if (is_numeric($id)) {
         $field = Field::find($id);
     } else {
         $field = Field::create();
         $field->setType($id);
     }
     if (!$field) {
         throw new NotFoundException(__('Field not found.'));
     }
     if (!($type = $userprofile->getType($field->type))) {
         throw new NotFoundException(__('Type not found.'));
     }
     return ['$view' => ['title' => __('Field'), 'name' => 'bixie/userprofile/admin/edit.php'], '$data' => ['field' => $field, 'type' => $type, 'roles' => array_values(Role::findAll())]];
 }
 /**
  * @Route("/edit")
  * @Request({"id"})
  * @Access("site: manage site", admin=true)
  */
 public function editAction($id = '')
 {
     /** @var \Bixie\Userprofile\UserprofileModule $userprofile */
     $userprofile = App::module('bixie/userprofile');
     if (is_numeric($id)) {
         $field = Field::find($id);
     } else {
         $field = Field::create();
         $field->setFieldType($id);
         $field->set('value', []);
         $field->set('data', []);
     }
     if (!$field) {
         throw new NotFoundException(__('Field not found.'));
     }
     if (!($type = $userprofile->getFieldType($field->type))) {
         throw new NotFoundException(__('Type not found.'));
     }
     $fixedFields = ['multiple', 'required', 'controls', 'repeatable'];
     if (!$field->id) {
         foreach ($type->getConfig() as $key => $value) {
             if (!in_array($key, $fixedFields)) {
                 $field->set($key, $value);
             }
         }
     }
     //check fixed value
     foreach ($fixedFields as $key) {
         if (!isset($type[$key])) {
             $type[$key] = 0;
         }
         if ($type[$key] != -1) {
             $field->set($key, $type[$key]);
         }
     }
     return ['$view' => ['title' => __('Field'), 'name' => 'bixie/userprofile/admin/edit.php'], '$data' => ['field' => $field, 'type' => $type, 'roles' => array_values(Role::findAll())]];
 }