Example #1
0
    $fileErrorMsg = $_FILES["myfile"]["error"];
    //0 for false and 1 for true
    if (!$fileTmpLoc) {
        die(new ModelResponse(false, 'Please browse for a valid file'));
    }
    $destPath = "{$uploadPath}/{$fileName}";
    if (move_uploaded_file($fileTmpLoc, $destPath)) {
        $raw_data = CSV::Parse($destPath);
        // Get initial user list
        $currentUserList = new Models\UserList();
        // Initialize container
        $userList = new Models\UserList(false);
        // proceed with creation
        foreach ($raw_data as $row) {
            $user = new Models\User();
            if ($currentUserList->ContainsUsername($row['username'])) {
                $user = \Models\User::FindUsername(strtolower(trim($row['username'])));
            } else {
                $user->Absorb($row);
            }
            $userList->add($user);
        }
        if (!$userList->Create()) {
            die(ModelResponse::DataSaveFailed());
        }
        $response = new ModelResponse(true, 'Data import success!', $userList);
        die($response);
    } else {
        die(ModelResponse::Busy());
    }
}
Example #2
0
 /**
  * Save (auto-detec, e.g. insert or update) data in this model. Returns boolean if operation is successful.
  *
  * @param array $excludeFields (Optional) An array of model fields to be excluded from saving
  *
  * @return boolean
  */
 public function SaveAll(array $excludeFields = null)
 {
     $db = \DB::Instance();
     $db->pdo->beginTransaction();
     $properties = $this->GetDBReadyValues($excludeFields);
     // Check if this exists
     if ($this->Exists()) {
         $db->update($this->GetModelName(), $properties, ['id' => $this->id]);
     } else {
         // Create data
         $insertResult = $db->insert($this->GetModelName(), [$properties]);
         // Get the ID
         $this->id = $db->pdo->lastInsertId();
         if ($this->id == 0) {
             $this->SetState(ModelResponse::DataSaveFailed());
         }
     }
     $errorInfo = $db->pdo->errorInfo();
     $errorCode = intval($errorInfo[0]);
     if ($errorCode != 0) {
         $this->SetState(new ModelResponse(false, sprintf('[%s] %s', $errorCode, $errorInfo[1])));
     }
     // Fetch-back
     $data = $db->select($this->GetModelName(), '*', ['id' => $this->id]);
     // If has result, absorb it
     if (sizeof($data) > 0) {
         $this->Absorb($data[0]);
     }
     // Commit transaction
     $db->pdo->commit();
     return true;
 }