/**
  * Create a new group.needle for all locales, or some of them.
  * If a row of group, needle and locale already exists, new row will be ignored.
  * 
  * @param string $groupDotNeedle E.g., 'fruits.apple'
  * @param array $texts Texts for locales. E.g., ['es' => 'manzana', 'en' => 'apple']
  * @param array $extra Extra fillable columns. E.g., ['type' => 'home']
  */
 public function create($localeGroupNeedle, $texts, array $extra = [])
 {
     $pointer = $this->parseLocaleGroupNeedle($localeGroupNeedle);
     //Created locales list. It will be used as a parameter for event
     $locales = [];
     if (is_null($pointer->needle)) {
         return false;
     }
     //Extra paramenter can not have sensitive attributes. So, we remove them.
     $extra = $this->removeSensitiveAttributes($extra);
     if (!is_null($pointer->locale)) {
         if (!is_array($texts)) {
             if ($this->model->where('group', $pointer->group)->where('needle', $pointer->needle)->where('locale', $pointer->locale)->count() < 1) {
                 $this->model->create(array_merge($extra, ['locale' => $pointer->locale, 'group' => $pointer->group, 'needle' => $pointer->needle, 'text' => $texts]));
                 $locales[] = $pointer->locale;
             }
         }
     } else {
         foreach ($texts as $locale => $text) {
             //Duplicates avoid
             if ($this->model->where('group', $pointer->group)->where('needle', $pointer->needle)->where('locale', $locale)->count() > 0) {
                 continue;
             }
             $this->model->create(array_merge($extra, ['locale' => $locale, 'group' => $pointer->group, 'needle' => $pointer->needle, 'text' => $text]));
             $locales[] = $locale;
         }
     }
     if (count($locales)) {
         $this->event->fire('translator.created', [$locales, $pointer->group, $pointer->needle]);
     }
 }
示例#2
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Model::unguard();
     DB::table('translation')->delete();
     $items = array(['english' => 'This', 'armenian' => 'այս', 'persian' => 'اس'], ['english' => 'That', 'armenian' => 'որ', 'persian' => 'کہ']);
     foreach ($items as $item) {
         Translation::create($item);
     }
     Model::reguard();
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $validator = \Validator::make($request->all(), ['body' => 'required|unique:translations,body']);
     if ($validator->passes()) {
         $translation = Translation::create(['body' => $request->input('body')]);
         $response = response()->json(['id' => $translation->getId()], 201);
     } else {
         $response = response()->json(['errors' => $validator->messages()->all()], 400);
     }
     return $response;
 }
 public function save(array $options = [])
 {
     // before save code
     parent::save($options);
     // after save code
     $this->Translations()->delete();
     foreach ($this->translatedAttributes as $field) {
         if (is_array($this->{$field})) {
             foreach ($this->{$field} as $k => $v) {
                 \App\Translation::create(['table' => 'groupes', 'foreign_id' => $this->id, 'lang' => $k, 'field' => $field, 'value' => $v]);
             }
         }
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $validator = \Validator::make($request->all(), ['body' => 'required', 'translation_id' => 'sometimes|exists:translations,id', 'via_dictionary' => 'sometimes|boolean']);
     if ($validator->passes()) {
         if ($request->input('via_dictionary')) {
             $definitions = \YaDictionary::lookup($request->input('body'));
             if ($definitions) {
                 foreach ($definitions as $definition) {
                     $partOfSpeech = $definition->getPartOfSpeech();
                     if (!($position = Position::where('body', $partOfSpeech)->first())) {
                         $position = Position::create(['body' => $partOfSpeech]);
                     }
                     if (!Word::where('body', $definition->getText())->where('position_id', $position->id)->first()) {
                         $word = new Word();
                         $word->body = $definition->getText();
                         $word->ts = $definition->getTranscription();
                         $word->position()->associate($position);
                         $word->save();
                         $translationIds = [];
                         foreach ($definition->getTranslations() as $yaTranslation) {
                             if (!($translation = Translation::where('body', $yaTranslation->getText())->first())) {
                                 $translation = Translation::create(['body' => $yaTranslation->getText()]);
                             }
                             $translationIds[] = $translation->id;
                         }
                         $word->translations()->attach($translationIds);
                     }
                 }
                 $response = response('This word has created from the dictionary', 201);
             } else {
                 $response = response()->json(['errors' => ['This word has not found in the dictionary.']], 404);
             }
         } else {
             if ($translationId = $request->input('translation_id')) {
                 $word = Word::create(['body' => $request->input('body')]);
                 $word->translations()->attach($translationId);
                 $response = response()->json(['id' => $word->getId()], 201);
             } else {
                 $response = response()->json(['errors' => ['It needs a translation.']], 400);
             }
         }
     } else {
         $response = response()->json(['errors' => $validator->messages()->all()], 400);
     }
     return $response;
 }