/**
  * Returns an array of location names
  */
 public static function astArrayForLocationList()
 {
     $astLocationResponseTypes = astPhoneLocationModel::all();
     $astArrayForLocationList = [];
     foreach ($astLocationResponseTypes as $astLocationResponseType) {
         $astArrayForLocationList[$astLocationResponseType->PHONE_LOCATION_ID] = $astLocationResponseType->PHONE_LOCATION_NAME;
     }
     return $astArrayForLocationList;
 }
 /**
  * Import a CSV
  */
 public function importPhones()
 {
     if (Input::hasfile('importfile')) {
         $inputFile = Input::file('importfile');
         ini_set("auto_detect_line_endings", true);
         // this is required to handle weird line-endings from Mac
         $csvfile = fopen($inputFile->getRealPath(), "r");
         $importedPhones = [];
         $failedPhones = [];
         $failedParameters = [];
         $successLineNumbers = [];
         $failedLineNumbers = [];
         $unknownParameters = [];
         $unknownParametersNames = [];
         for ($x = 0; $x < 1; $x++) {
             $parameters = fgetcsv($csvfile);
             $templateIndex = array_search("template", $parameters);
             $modelIndex = array_search("output", $parameters);
             $locationIndex = array_search("_ignoreLocation", $parameters);
             $firstLastIndex = array_search("_FIRST-LAST_", $parameters);
             $linelabelIndex = array_search("_LINE-LABEL1_", $parameters);
         }
         $lineNumber = 2;
         while (!feof($csvfile)) {
             $columns = fgetcsv($csvfile);
             // TEMPLATE
             $templateName = $columns[$templateIndex];
             $template = astPhoneTemplateModel::where('TEMPLATE_NAME', '=', $templateName)->get();
             // MODEL
             $psn = explode('-', $columns[$modelIndex])[0];
             $model = astPhoneModelModel::where('PSN', '=', $psn)->get();
             // LOCATION
             $phoneLocation = astPhoneLocationModel::where('PHONE_LOCATION_NAME', '=', $columns[$locationIndex])->get();
             // MAC
             $modelMac = $columns[$modelIndex];
             $temp = strpos($modelMac, '-');
             $mac = substr($modelMac, $temp + 1);
             // FIRST_LAST
             $firstLast = $columns[$firstLastIndex];
             // LINE-LABEL1
             $linelabel = $columns[$linelabelIndex];
             // check if a phone with the same MAC address already exists
             $macCheck = astPhoneModel::where('MAC', '=', $mac)->get();
             //does not go through if does not have template, model, location, or if a phone with the same MAC address already exists
             if (count($template) > 0 && count($model) > 0 && count($phoneLocation) > 0 && count($macCheck) == 0) {
                 // new record for phone
                 $newPhoneRecord = ["PHONE_TEMPLATE_ID" => $template[0]->PHONE_TEMPLATE_ID, "DESCRIPTION" => $firstLast . ' ' . $linelabel, "PHONE_MODEL_ID" => $model[0]->PHONE_MODEL_ID, "PHONE_LOCATION_ID" => $phoneLocation[0]->PHONE_LOCATION_ID, "MAC" => $mac];
                 $astPhone = astPhoneModel::create($newPhoneRecord);
                 array_push($importedPhones, $columns[1]);
                 $successLineNumbers[$columns[1]] = $lineNumber;
                 // keep track of index for $parameters
                 $i = 0;
                 // new records for parameter values
                 foreach ($columns as $column) {
                     if ($column != '') {
                         //corect DLST value
                         if ($column[0] == '"') {
                             $column = trim($column, '"');
                         }
                         $parameterName = $parameters[$i];
                         if ($parameters[$i][0] == '_') {
                             $parameterName = ltrim($parameterName, '_');
                         }
                         if (substr($parameters[$i], -1) == '_') {
                             $parameterName = rtrim($parameterName, '_');
                         }
                         $parameter = astParameterModel::where('NAME', '=', $parameterName)->get();
                         if (count($parameter) > 0) {
                             $newPhoneParameterValueRecord = ["PHONE_ID" => $astPhone->PHONE_ID, "PARAMETER_ID" => $parameter[0]->PARAMETER_ID, "VALUE" => $column];
                             astPhoneParameterValueModel::create($newPhoneParameterValueRecord);
                         } else {
                             if ($parameterName != 'template' && $parameterName != 'output') {
                                 $unknownParameter = ["name" => $parameterName, "PHONE_ID" => $astPhone->PHONE_ID, "PARAMETER_ID" => null, "VALUE" => $column];
                                 array_push($unknownParameters, $unknownParameter);
                                 // for confirmImport view so that same parameter doesn't get repeated to check off that it is needed
                                 if (!in_array($parameterName, $unknownParametersNames)) {
                                     array_push($unknownParametersNames, $parameterName);
                                 }
                             }
                         }
                     }
                     $i++;
                 }
             } else {
                 if ($columns[1] != null) {
                     array_push($failedPhones, $columns[1]);
                     $failedLineNumbers[$columns[1]] = $lineNumber;
                     if (!(count($template) > 0)) {
                         $failedParameters[$columns[1]] = 'template';
                     } elseif (!(count($model) > 0)) {
                         $failedParameters[$columns[1]] = 'model';
                     } elseif (!(count($phoneLocation) > 0)) {
                         $failedParameters[$columns[1]] = 'location';
                     } elseif (!(count($macCheck) == 0)) {
                         $failedParameters[$columns[1]] = 'mac';
                     }
                 }
             }
             $lineNumber++;
         }
         if (count($unknownParameters) != 0) {
             session()->put('unknownParameters', $unknownParameters);
             session()->put('unknownParametersNames', $unknownParametersNames);
         }
         return View::make('ast.astPhones.confirmImport')->with('importedPhones', $importedPhones)->with('successLineNumbers', $successLineNumbers)->with('failedPhones', $failedPhones)->with('failedParameters', $failedParameters)->with('failedLineNumbers', $failedLineNumbers)->with('unknownParameters', $unknownParameters)->with('unknownParametersNames', $unknownParametersNames);
     } else {
         Session::flash('unsuccessfulMessage', 'Need to select a CSV file!');
         return Redirect::route('importCSV');
     }
 }
 /**
  * Delete an existing location if no phone uses the model.
  */
 public function delete($id)
 {
     $astPhonesWithLocation = astPhoneModel::where('PHONE_LOCATION_ID', '=', $id)->get();
     if (count($astPhonesWithLocation) == 0) {
         $astPhoneLocation = astPhoneLocationModel::findOrFail($id);
         $astPhoneLocation->delete();
         Session::flash('astMessage', 'Location has been deleted!');
         return Redirect::route('locations');
     } else {
         Session::flash('astDeleteMessage', 'In order to delete this location, all phones with this location must be deleted first.');
         return redirect()->route('showLocation', [$id]);
     }
 }