php - Returning a View from a Model in Laravel 5 -
my controller
creates instance of class, soapwrapper.php
, contains function retrieves data web service. web service allows gather 100 records per query, finding out total number of records query , looping , calling web service query every 100 records data.
to give kind of feedback user request being processed (a loading screen of sorts), return view
displays html
box text says loading record x of y
, y
total number of records, , x
updated @ each iteration of loop.
here current model
, soapwrapper.php
:
<?php namespace app\models; use soapclient; use illuminate\http\redirectresponse; use redirect; class soapwrapper { // soap exchange wos authentication public $auth_client; public $auth_response; // soap exchange wos search public $search_client; public $search_response; // number of records found public $len; // xml data send soap request wos public $data; // array store records public $records = []; // function iterate , store relevant data returned soap exchange public function iteratewossearch($submit) { // variable store average time/record retrieval (based on calculations) $avg = 0.08; // create variable store time loading screen $timedecimal = (round((($submit->len)*$avg), 2)); // create array represent citation values ignore, i.e. not interested // in publications less 4 citations $ignore = array(0, 1, 2, 3); // create counter variable use progress bar $counter = 1; // turn time readable format (mins & secs, rather secs) if ($timedecimal > 59.99) { $minutes = round(($timedecimal/60), 0, php_round_half_down); while ($timedecimal > 59.99) { $timedecimal -= 60; $seconds = round($timedecimal, 0); }; } else { $minutes = 0; $seconds = round($timedecimal, 0); }; // iterate through records, perform search each 100 records (max per call) // , tabulate data ($i = 1; $i <= ($submit->len); $i+=100) { // set search parameters current iteration (first record = 1, 101, 201, 301 etc.) $submit->data['retrieveparameters']['firstrecord'] = $i; // gather search response current iteration try { $submit->search_response = $submit->search_client->search($submit->data); } catch (exception $e) { echo $e->getmessage(); }; // turn soap client object current response xml element $xml = simplexml_load_string($submit->search_response->return->records); // save variable names citations, country , publication year $citations = ""; $pubyear = ""; $country = ""; // iterate through current data set , store $records array foreach($xml->rec $record) { // create authors array rec data $authors = []; return view::make('loading', array('minutes' => $minutes, 'seconds' => $seconds, 'counter' => $counter)); // authors foreach($record->static_data->summary->names->name $thisauthor) { array_push($authors, $thisauthor->full_name); } // country (if exists) if (isset($record->static_data->item->reprint_contact->address_spec->country)) { $country = (string)$record->static_data->item->reprint_contact->address_spec->country; } else { $country = ""; }; // set current publication year $pubyear = (string)$record->static_data->summary->pub_info->attributes()->pubyear; // number of citations, if 0-3 ($ignore array) 'break' out of loop entirely if (!in_array($record->dynamic_data->citation_related->tc_list->silo_tc->attributes(), $ignore)) { $citations = (string)$record->dynamic_data->citation_related->tc_list->silo_tc->attributes(); } else { // break both loops break 2; }; // iteration map values recorded temporary array variable, // $arecord (equivalent 1 row of data in table) $arecord = [ "authors" => $authors, "id" => "", "pubyear" => $pubyear, "country" => $country, "citations" => $citations ]; // pass data iteration array variable '$records', // after iterations, each element in $records single // record or row of data single journal array_push($submit->records, $arecord) ; } // increment next record $counter+=100; } } };
then view
called loading
called @ each iteration of loop, return estimated time query take passing in <?php echo $minutes ?>
, , same seconds, , current value of $counter
reflect set of records it's processing:
<p>loading record <?php echo $counter ?> of <?php echo $len ?></p>
is possible return view
within model
? i'm not sure how else can create 'loading' page this.
Comments
Post a Comment