Created
October 3, 2012 07:50
-
-
Save rakeshtembhurne/3825667 to your computer and use it in GitHub Desktop.
php import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Action method used to import talks | |
* | |
* @return void | |
*/ | |
public function import() | |
{ | |
// Build and initialize needed variables | |
$csv = $this->request->data['Talk']['csv']; | |
$error = null; | |
$failedRows = array(); | |
$rows = array( | |
'added' => 0, | |
'edited' => 0, | |
); | |
$skipFirstRow = (bool)$this->request->data['Talk']['skip_first_row']; | |
if (4 == $csv['error'] || !is_uploaded_file($csv['tmp_name'])) { | |
$error = __('Please upload a CSV file to import talks'); | |
} else if (0 == $csv['error']) { | |
$counter = 1; | |
$handle = fopen($csv['tmp_name'], 'r'); | |
// Skip first row, if user said so | |
if ($skipFirstRow) { | |
fgetcsv($handle); | |
} | |
// Import | |
while (false !== ($row = fgetcsv($handle))) { | |
if (!$this->__import($row, $rows)) { | |
$failedRows[] = __('Talk #%1$d - %2$s', $counter, $this->Talk->cleanData($row[0])); | |
} | |
$counter++; | |
} | |
fclose($handle); | |
if (0 == $rows['added'] && 0 == $rows['edited']) { | |
$error = __('No talks to import'); | |
} | |
} | |
$this->__afterImport($error, $failedRows, $rows); | |
}//end import() | |
/** | |
* Method used to do things after import finished | |
* | |
* @param string $error Error message | |
* @param array $failedRows Failed talks | |
* @param array $rows Count for added and updated talks | |
* | |
* @return void | |
*/ | |
private function __afterImport($error, $failedRows, $rows) | |
{ | |
// By default, redirect to 'talks list' page | |
$redirectUrl = array('action' => 'index'); | |
// Handle success and failure | |
if (empty($error)) { | |
// Build message for added/edited data | |
if (0 < $rows['added'] && 0 < $rows['edited']) { | |
$message = __('Talks imported, %1$d added and %2$d updated', $rows['added'], $rows['edited']); | |
} else if (0 < $rows['added']) { | |
$message = __('%d talks imported and added', $rows['added']); | |
} else { | |
$message = __('%d talks imported and updated', $rows['edited']); | |
} | |
// Build message for failed data | |
if (!empty($failedRows)) { | |
$failedTalks = count($failedRows); | |
$failedRows = implode('<br />', $failedRows); | |
$failedRows = '<b>' . __('Talks that failed to import') . '</b><br />' . $failedRows; | |
$this->_setFlash('failedTalks', $failedRows); | |
$message .= ' - '; | |
$message .= __('%d talks failed to import, you will need to add them manually', $failedTalks); | |
$message .= ', <a id="linkToShowFailedTalks" href="#failedTalksMessage">' . __('<strong>see them</strong>'); | |
$message .= '</a>'; | |
} | |
$this->_setSuccessFlash($message); | |
} else { | |
$this->_setErrorFlash($error); | |
$redirectUrl['#'] = 'import'; | |
} | |
// Redirect to desired page | |
$this->redirect($redirectUrl); | |
}//end __afterImport() | |
/** | |
* Method used to import talk | |
* | |
* @param array $row Talk to import | |
* @param array &$rows Statistics of imported rows, to update | |
* | |
* @return boolean If talk imported then returns true otherwise false | |
*/ | |
private function __import($row, &$rows) | |
{ | |
// Each row must have talk title and speaker name | |
if (!isset($row[0]) || !isset($row[1])) { | |
return false; | |
} | |
// Build data to import | |
$conditionsOrData = array( | |
'conference_id' => CONFERENCE_ID, | |
'speaker_name' => trim((string)$row[1]), | |
'title' => trim((string)$row[0]), | |
); | |
$conditionsOrData += array( | |
'description' => (isset($row[4]) ? trim((string) $row[4]) : ''), | |
'id' => (int)$this->Talk->field('Talk.id', $conditionsOrData), | |
'speaker_email' => (isset($row[2]) ? trim((string)$row[2]) : ''), | |
'speaker_website' => (isset($row[3]) ? trim((string)$row[3]) : ''), | |
'track' => (isset($row[6]) ? trim((string)$row[6]) : ''), | |
'type' => (isset($row[5]) ? trim((string)$row[5]) : ''), | |
); | |
// Import talk | |
$this->Talk->create(); | |
if ($this->Talk->save($conditionsOrData)) { | |
if (0 < $conditionsOrData['id']) { | |
$rows['edited']++; | |
} else { | |
$rows['added']++; | |
} | |
return true; | |
} | |
// By default, return false | |
return false; | |
}//end __import() | |
// Model Method | |
public function cleanData($clean, $type = false) | |
{ | |
App::uses('Sanitize', 'Utility'); | |
if ($type == 'description') { | |
$cleaned = Sanitize::clean($clean, array('encode' => false, 'escape' => false)); | |
$cleaned = strip_tags($cleaned, '<strong><bold><em><i>'); | |
} else if ($type == 'email' OR $type == 'url') { | |
$cleaned = strip_tags($clean); | |
} else { | |
$cleaned = Sanitize::clean($clean, array('encode' => false, 'escape' => false)); | |
$cleaned = strip_tags($clean); | |
//$cleaned = preg_replace('/[^a-zA-Z0-9\s]/', '', $cleaned); | |
} | |
return $cleaned; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment