private function removeContact()
 {
     if ($this->get_request_method() != "POST") {
         $this->response('', 406);
     }
     $phonebook = new Phonebook();
     $data = json_decode(file_get_contents('php://input'), true);
     $phonebook->deleteContactById($data["id"]);
     $this->response('', 200);
 }
 private function doDeleteType()
 {
     $currentTs = time();
     $identity = \Yii::$app->user->getIdentity();
     $arrIds = \Yii::$app->request->post('ids');
     if (is_array($arrIds) && !empty($arrIds)) {
         $query = Phonebook::find();
         $query->where(["id" => $arrIds]);
         $lst = $query->all();
         $deleted = 0;
         if ($lst) {
             foreach ($lst as $Object) {
                 $Object->status = Workflow::STATUS_REJECTED;
                 if ($Object->save()) {
                     Yii::info(json_encode(array('id' => $Object->id, 'userId' => $identity->id, 'status' => $Object->status, 'ts' => $currentTs)), 'audit.faq.update.' . $Object->id);
                     $deleted = $deleted + 1;
                 }
             }
         }
         if ($deleted > 0) {
             UiMessage::setMessage("ลบข้อมูลจำนวน {$deleted} รายการ", 'success');
         } else {
             UiMessage::setMessage('ไม่มีข้อมูลถูกลบ');
         }
     }
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return Phonebook the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = Phonebook::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function testPhonebookCRUDTest()
 {
     $phonebook = new Phonebook($this->container->getDbConnection());
     $this->assertEquals('id', $phonebook->getPK());
     // Create new contact
     $phonebook->setFirstName('Test');
     $phonebook->setLastName('Contact');
     $phonebook->setBirthDate('1911-11-11');
     $phonebook->setPhoneNumber('+1234567890');
     $id = $phonebook->insert();
     $this->assertEquals(6, $id);
     // Read the new contact from the db
     $contact = $phonebook->getByField('id', 6)[0];
     $this->assertEquals('Test', $contact['first_name']);
     $this->assertEquals('Contact', $contact['last_name']);
     $this->assertEquals('1911-11-11', $contact['birthdate']);
     $this->assertEquals('+1234567890', $contact['phone_number']);
     // Update the new contact and check the db is updated correctly
     $phonebook->setId(6);
     $phonebook->setLastName('Edited');
     $phonebook->setBirthdate('1922-02-02');
     $phonebook->update();
     $contact = $phonebook->getByField('id', 6)[0];
     $this->assertEquals('Test', $contact['first_name']);
     $this->assertEquals('Edited', $contact['last_name']);
     $this->assertEquals('1922-02-02', $contact['birthdate']);
     $this->assertEquals('+1234567890', $contact['phone_number']);
     // Delete the new contact and associated data from the db; check that it can't be read anymore
     $contact = $phonebook->deleteById(6);
     $contact = $phonebook->getByField('id', 6);
     $this->assertEmpty($contact);
     // test getAll() to count centers
     $allContacts = $phonebook->getAll();
     $this->assertCount(3, $allContacts);
 }
 private function readContactDataFromPost(Phonebook $contact)
 {
     $contact->setFirstName($_POST['first_name']);
     $contact->setLastName($_POST['last_name']);
     $contact->setBirthdate($_POST['birthdate']);
     $contact->setPhoneNumber($_POST['phone_number']);
 }