Example #1
0
 function actionIndex()
 {
     $svg = file_get_contents(dirname(__FILE__) . '/../bober-template.svg');
     $data = array(array('###IME_IN_PRIIMEK###' => 'Dean Gostiša'), array('###IME_IN_PRIIMEK###' => 'Janez Novak'), array('###IME_IN_PRIIMEK###' => 'Franci na Balanci'), array('###IME_IN_PRIIMEK###' => 'Zvonko Boštjančič'), array('###IME_IN_PRIIMEK###' => 'Neža Čeč'), array('###IME_IN_PRIIMEK###' => 'Gašper Černevšek'), array('###IME_IN_PRIIMEK###' => 'Tatjana Per'));
     $pdf = Svg2Pdf::BatchConvert($svg, $data);
     $name = 'Seznam priznanj.pdf';
     header('Content-Type: application/x-download');
     header('Content-Disposition: attachment; filename="' . $name . '"');
     header('Cache-Control: private, max-age=0, must-revalidate');
     header('Pragma: public');
     header('X-Content-Type-Options: nosniff');
     header('X-Frame-Options: SAMEORIGIN');
     // connection close if you want that file is loaded fast (Dean Gostiša)
     header('Connection: close');
     ini_set('zlib.output_compression', '0');
     header('Content-Length: ' . strlen($pdf));
     echo $pdf;
     die;
 }
Example #2
0
 /**
  * Creates a new item
  * 
  * @access public
  * @return void
  */
 public function actionCreate()
 {
     $this->_checkAuth();
     $currentModel = mb_strtolower($_GET['model'], 'UTF-8');
     $return_array = array();
     $return_custom = false;
     switch ($currentModel) {
         // Get an instance of the respective model
         case 'convert':
             $model = new Convert();
             break;
         default:
             $this->_sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>', $_GET['model']));
             exit;
     }
     // Try to assign POST values to attributes
     foreach ($_POST as $var => $value) {
         // Does the model have this attribute?
         if ($model->hasAttribute($var)) {
             $model->{$var} = $value;
         } else {
             // No, raise an error
             $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
         }
     }
     if ($currentModel == 'convert') {
         $model->user_id = $this->user_id;
         $return_custom = true;
         if ($model->data_encoding != '' && $model->data_encoding != null) {
             $model->data_encoding = trim(mb_strtoupper($model->data_encoding, 'UTF-8'));
             if (!Encoding::EncodingExists($model->data_encoding)) {
                 $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not valid for model <b>%s</b>. Valid values: ' . implode(', ', Encoding::$list), 'data_encoding', $_GET['model']));
             }
         } else {
             $model->data_encoding = null;
         }
         if ($model->data_encoding != null) {
             $model->data = iconv($model->data_encoding, 'UTF-8', $model->data);
         }
         switch (mb_strtolower($model->data_type, 'UTF-8')) {
             case 'json':
                 if ($model->data != '') {
                     $list = json_decode($model->data, true);
                     // check if array is designed as: array( array("###KEY_TO_REPLACE###" => "Replaced value"), array(...))
                     $keys = array_keys($list);
                     $keys_ok = true;
                     for ($i = 0; $i < count($keys); ++$i) {
                         if (!is_numeric($keys[$i])) {
                             $keys_ok = false;
                         }
                     }
                     if ($keys_ok) {
                         $model->data_to_use = base64_encode(serialize($list));
                     } else {
                         $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is invalid json format for model <b>%s</b>. JSON structure should be: [{"###KEY_TO_REPLACE###":"Replaced Value 1","###KEY###":"Key 1"},{"###KEY_TO_REPLACE###":"Replaced Value 2","###KEY###":"Key 2"}]', 'data', $_GET['model']));
                     }
                 } else {
                     $model->data_to_use = base64_encode(serialize(array()));
                 }
                 break;
             case 'csv':
                 $delimiter = ';';
                 $enclosure = '"';
                 if ($model->data_parameters != null && $model->data_parameters != '') {
                     $params = explode('||', $model->data_parameters);
                     foreach ($params as $param) {
                         $param = explode('==', $param);
                         $key = $param[0];
                         $value = isset($param[1]) ? $param[1] : '';
                         switch ($key) {
                             case 'delimiter':
                                 $delimiter = $value;
                                 break;
                             case 'enclosure':
                                 $enclosure = $value;
                                 break;
                             default:
                                 $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is invalid format for model <b>%s</b>. Valid value for CSV is: \'setting1==value||setting2==value\', example: \'delimiter==;||enclosure=="\', default delimiter is: \';\', default enclosure is: \'"\'', 'data_parameters', $_GET['model']));
                                 break;
                         }
                     }
                 }
                 $data = CSV::toArray($model->data, $delimiter, $enclosure);
                 if (count($data) > 0) {
                     if (count($data) > 1) {
                         $header = $data[0];
                         $list = array();
                         for ($i = 1; $i < count($data); ++$i) {
                             $row = array();
                             for ($c = 0; $c < count($header); ++$c) {
                                 $row[$header[$c]] = isset($data[$c]) ? $data[$c] : '';
                             }
                             $list[] = $row;
                         }
                         $model->data_to_use = base64_encode(serialize($list));
                     } else {
                         $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is empty for model <b>%s</b>. CSV should contain more lines. Not only one header line.', 'data', $_GET['model']));
                     }
                 } else {
                     $model->data_to_use = base64_encode(serialize(array()));
                 }
                 break;
             default:
                 $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is invalid for model <b>%s</b>, allowed values: [json, csv]', 'data_type', $_GET['model']));
                 break;
         }
     }
     // Try to save the model
     if ($model->save()) {
         // Saving was OK
         if ($currentModel == 'convert') {
             if ($model->background == 1) {
                 // we send work to background, user can pool status of convert
                 $jobSetId = Svg2Pdf::BatchConvert(base64_decode($model->svg), unserialize(base64_decode($model->data_to_use)), $model->background);
                 $model->backgroud_job_set_id = $jobSetId;
                 $model->save();
             } else {
                 // we send work to do it now, user wait for result in open connection
                 $pdf = base64_encode(Svg2Pdf::BatchConvert(base64_decode($model->svg), unserialize(base64_decode($model->data_to_use))));
                 $model->result = $pdf;
                 $model->save();
             }
             // do not return all data
             $return_array = array('id' => $model->id, 'background' => $model->background, 'result' => $model->result);
         }
         $this->_sendResponse(200, $this->_getObjectEncoded($_GET['model'], $return_custom ? $return_array : $model->attributes));
     } else {
         // Errors occurred
         $msg = "<h1>Error</h1>";
         $msg .= sprintf("Couldn't create model <b>%s</b>", $_GET['model']);
         $msg .= "<ul>";
         foreach ($model->errors as $attribute => $attr_errors) {
             $msg .= "<li>Attribute: {$attribute}</li>";
             $msg .= "<ul>";
             foreach ($attr_errors as $attr_error) {
                 $msg .= "<li>{$attr_error}</li>";
             }
             $msg .= "</ul>";
         }
         $msg .= "</ul>";
         $this->_sendResponse(500, $msg);
     }
     var_dump($_REQUEST);
 }