Example #1
0
 /**
  * Saves notes passed in Json and returns updated notes as Json
  * @return Array of objects
  */
 public function actionSaveNotes()
 {
     $saved = false;
     $this->_setJsonResponse();
     if (!Yii::$app->request->getIsPost()) {
         throw new NoteControllerInvalidCallException('Invalid call... ' . __METHOD__ . ' takes in only POST requests.');
     }
     $noteData = Json::decode(Yii::$app->request->getPost('notes'));
     foreach ($noteData as $data) {
         $note = Note::find($data['id']);
         if (is_null($note)) {
             $note = new Note();
         }
         $changed = $note->saveUpdateable($data);
         if ($changed) {
             $saved = true;
         }
     }
     # If there were db changes we want to remove any leftover tags...
     if ($saved) {
         Tag::eliminateOrphans();
     }
     # Tags
     $tagData = Json::decode(Yii::$app->request->getPost('tags'));
     $tags = Tag::getAll(true);
     # Get tag data as array
     # Map Tag statuses
     if (count($tagData) > 0) {
         Tag::mapStatuses($tags, $tagData);
     }
     # Reload Note's array
     $notes = Note::getAll(true);
     # Reorder Note array
     Note::reorder($notes, $noteData);
     return array('notes' => $notes, 'tags' => $tags);
 }