/** * Remove data matching $path from the $data array. * You can use `{n}` and `{s}` to remove multiple elements * from $data. * * @param array $data The data to operate on * @param string $path A path expression to use to remove. * @return array The modified array. * @link http://book.cakephp.org/3.0/en/core--libraries/hash.html#Hash::remove */ public static function remove(array $data, $path) { $noTokens = strpos($path, '[') === false; $noExpansion = strpos($path, '{') === false; if ($noExpansion && $noTokens && strpos($path, '.') === false) { unset($data[$path]); return $data; } $tokens = $noTokens ? explode('.', $path) : Text::tokenize($path, '.', '[', ']'); if ($noExpansion && $noTokens) { return static::_simpleOp('remove', $data, $tokens); } $token = array_shift($tokens); $nextPath = implode('.', $tokens); list($token, $conditions) = self::_splitConditions($token); foreach ($data as $k => $v) { $match = static::_matchToken($k, $token); if ($match && is_array($v)) { if ($conditions && static::_matches($v, $conditions)) { unset($data[$k]); continue; } $data[$k] = static::remove($v, $nextPath); if (empty($data[$k])) { unset($data[$k]); } } elseif ($match && empty($nextPath)) { unset($data[$k]); } } return $data; }
/** * testTokenize method * * @return void */ public function testTokenize() { $result = Text::tokenize('A,(short,boring test)'); $expected = ['A', '(short,boring test)']; $this->assertEquals($expected, $result); $result = Text::tokenize('A,(short,more interesting( test)'); $expected = ['A', '(short,more interesting( test)']; $this->assertEquals($expected, $result); $result = Text::tokenize('A,(short,very interesting( test))'); $expected = ['A', '(short,very interesting( test))']; $this->assertEquals($expected, $result); $result = Text::tokenize('"single tag"', ' ', '"', '"'); $expected = ['"single tag"']; $this->assertEquals($expected, $result); $result = Text::tokenize('tagA "single tag" tagB', ' ', '"', '"'); $expected = ['tagA', '"single tag"', 'tagB']; $this->assertEquals($expected, $result); }
/** * testTokenize method * * @return void */ public function testTokenize() { $result = Text::tokenize('A,(short,boring test)'); $expected = ['A', '(short,boring test)']; $this->assertEquals($expected, $result); $result = Text::tokenize('A,(short,more interesting( test)'); $expected = ['A', '(short,more interesting( test)']; $this->assertEquals($expected, $result); $result = Text::tokenize('A,(short,very interesting( test))'); $expected = ['A', '(short,very interesting( test))']; $this->assertEquals($expected, $result); $result = Text::tokenize('"single tag"', ' ', '"', '"'); $expected = ['"single tag"']; $this->assertEquals($expected, $result); $result = Text::tokenize('tagA "single tag" tagB', ' ', '"', '"'); $expected = ['tagA', '"single tag"', 'tagB']; $this->assertEquals($expected, $result); // Ideographic width space. $result = Text::tokenize("tagA \"single tag\" tagB", " ", '"', '"'); $expected = ['tagA', '"single tag"', 'tagB']; $this->assertEquals($expected, $result); }
public function import() { $submissionFile = ''; $table = ''; //get form data if ($this->request->is('post')) { $submissionFile = $this->request->data['submissionFile']; $table = $this->request->data['table']; } //process if ($this->request->is('post')) { if ($submissionFile == '') { $this->set('file_status', 'No file submitted, try again.'); } elseif ($submissionFile) { //read in file data $file = new File($submissionFile['tmp_name']); $rawFileData = $file->read(true, 'r'); $lineArr = $array = preg_split("/\r\n|\n|\r/", $rawFileData); //parse file data line by line for appropriate table $this->set('file_status', 'File submitted.'); if ($table == 0) { $ticketsTable = TableRegistry::get('Tickets'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $ticket = $ticketsTable->newEntity(); $ticket->id = $dataFields[0]; $ticket->theater_id = $dataFields[1]; $ticket->section_id = $dataFields[2]; $ticket->row_id = $dataFields[3]; $ticket->seat_id = $dataFields[4]; $ticket->status = $dataFields[5]; $ticket->customer_id = $dataFields[6]; $ticket->customer_name = $dataFields[7]; $ticket->ticket_number = $dataFields[8]; $ticket->performance_id = $dataFields[9]; $ticket->season_id = $dataFields[10]; $ticket->purchase_id = $dataFields[11]; $ticketsTable->save($ticket); } } elseif ($table == 1) { $staffAssignmentsTable = TableRegistry::get('StaffAssignments'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $staffAssignment = $staffAssignmentsTable->newEntity(); $staffAssignment->id = $dataFields[0]; $staffAssignment->user_id = $dataFields[1]; $staffAssignment->theater_id = $dataFields[2]; $staffAssignment->access_level = $dataFields[3]; $staffAssignmentsTable->save($staffAssignment); } } elseif ($table == 2) { $usersTable = TableRegistry::get('Users'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $user = $usersTable->newEntity(); $user->id = $dataFields[0]; $user->name = $dataFields[1]; $user->street = $dataFields[2]; $user->city = $dataFields[3]; $user->state = $dataFields[4]; $user->zip = $dataFields[5]; $user->phone_number = $dataFields[6]; $user->email = $dataFields[7]; $user->password = $dataFields[8]; $user->is_super_admin = $dataFields[9]; $user->date_created = $dataFields[10]; $user->date_modified = $dataFields[11]; $user->salt = $dataFields[12]; $usersTable->save($user); } } elseif ($table == 3) { $theatersTable = TableRegistry::get('Theaters'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $theater = $theatersTable->newEntity(); $theater->id = $dataFields[0]; $theater->name = $dataFields[1]; $theater->sales_tax = $dataFields[2]; $theater->description = $dataFields[3]; $theater->location = $dataFields[4]; $theater->artwork = $dataFields[5]; $theatersTable->save($theater); } } elseif ($table == 4) { $sectionsTable = TableRegistry::get('Sections'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $section = $sectionsTable->newEntity(); $section->id = $dataFields[0]; $section->name = $dataFields[1]; $section->code = $dataFields[2]; $section->theater_id = $dataFields[3]; $section->accessible_section = $dataFields[4]; $section->front_text = $dataFields[5]; $section->back_text = $dataFields[6]; $sectionsTable->save($section); } } elseif ($table == 5) { $seatsTable = TableRegistry::get('Seats'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $seat = $seatsTable->newEntity(); $seat->id = $dataFields[0]; $seat->theater_id = $dataFields[1]; $seat->section_id = $dataFields[2]; $seat->row_id = $dataFields[3]; $seat->code = $dataFields[4]; $seat->price = $dataFields[5]; $seatsTable->save($seat); } } elseif ($table == 6) { $seasonsTable = TableRegistry::get('Seasons'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $season = $seasonsTable->newEntity(); $season->id = $dataFields[0]; $season->name = $dataFields[1]; $season->start_time = $dataFields[2]; $season->end_time = $dataFields[3]; $season->ticket_price = $dataFields[4]; $season->theater_id = $dataFields[5]; $season->about = $dataFields[6]; $seasonsTable->save($season); } } elseif ($table == 7) { $rowsTable = TableRegistry::get('Rows'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $row = $rowsTable->newEntity(); $row->id = $dataFields[0]; $row->theater_id = $dataFields[1]; $row->section_id = $dataFields[2]; $row->code = $dataFields[3]; $rowsTable->save($row); } } elseif ($table == 8) { $playsTable = TableRegistry::get('Plays'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $play = $playsTable->newEntity(); $play->id = $dataFields[0]; $play->name = $dataFields[1]; $play->artwork = $dataFields[2]; $play->description = $dataFields[3]; $play->author = $dataFields[4]; $play->shortname = $dataFields[5]; $playsTable->save($play); } } elseif ($table == 9) { $performancesTable = TableRegistry::get('Performances'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $performance = $performancesTable->newEntity(); $performance->id = $dataFields[0]; $performance->start_time = $dataFields[1]; $performance->open = $dataFields[2]; $performance->canceled = $dataFields[3]; $performance->play_id = $dataFields[4]; $performance->theater_id = $dataFields[5]; $performance->season_id = $dataFields[6]; $performancesTable->save($performance); } } elseif ($table == 10) { $cart_itemsTable = TableRegistry::get('Cart_items'); foreach ($lineArr as $line) { $dataFields = Text::tokenize($line); $cart_items = $cart_itemsTable->newEntity(); $cart_items->id = $dataFields[0]; $cart_items->cart_id = $dataFields[1]; $cart_items->performance_id = $dataFields[2]; $cart_items->seat_id = $dataFields[3]; $cart_items->season_id = $dataFields[4]; $cart_itemsTable->save($cart_items); } } else { $this->Flash->error('Unexpected table value'); } } else { $this->set('file_status', 'Please submit a file.'); } } else { $this->set('file_status', 'Please submit a file.'); } }