Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.31 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\components;
  4.  
  5. use Yii;
  6. use yii\base\Component;
  7. use yii\base\InvalidConfigException;
  8. use \app\models\User;
  9. use yii\imagine\Image;
  10. use Imagine\Gd;
  11. use Imagine\Image\Box;
  12. use Imagine\Image\BoxInterface;
  13.  
  14. class Util extends Component {
  15.  
  16. public $beforeBody;
  17. public $afterBody;
  18. public $member;
  19. public $tab = 1;
  20.  
  21. const PUBLISH = 1;
  22. const UNPUBLISH = 0;
  23.  
  24. public static $dirSample = '@webroot/xls_sample/';
  25. public static $dirParsing = '@webroot/xls_parsing/';
  26. public static $dirParsingRelative = '@web/xls_parsing/';
  27. public static $dirQrcode = '@webroot/qrcode/';
  28. public static $dirQrcodeRelative = '@web/qrcode/';
  29. public static $dirTemp = '@webroot/temp/';
  30. public static $dirTempRelative = '@web/temp/';
  31.  
  32. public function publish()
  33. {
  34. return ['Unpublish', 'Publish'];
  35. }
  36.  
  37. public function publishLabel($int)
  38. {
  39. $array = $this->publish();
  40. if ($int == 1)
  41. $class = 'success';
  42. else
  43. $class = 'default';
  44.  
  45. return '<div class="label label-' . $class . '">' . $array[$int] . '</div>';
  46. }
  47.  
  48. public function say()
  49. {
  50. return 'haiii';
  51. }
  52.  
  53. public function getUserId($id = 0)
  54. {
  55. if ($id)
  56. return User::find()->where(['id' => $id])->one();
  57. }
  58.  
  59. public function randomString($length = 10, $chars = '', $type = array())
  60. {
  61. $alphaSmall = 'abcdefghijklmnopqrstuvwxyz';
  62. $alphaBig = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  63. $num = '0123456789';
  64. $othr = '`~!@#$%^&*()/*-+_=[{}]|;:",<>.\/?' . "'";
  65. $characters = "";
  66. $string = '';
  67. isset($type['alphaSmall']) ? $type['alphaSmall'] : $type['alphaSmall'] = true;
  68. isset($type['alphaBig']) ? $type['alphaBig'] : $type['alphaBig'] = true;
  69. isset($type['num']) ? $type['num'] : $type['num'] = true;
  70. isset($type['othr']) ? $type['othr'] : $type['othr'] = false;
  71. isset($type['duplicate']) ? $type['duplicate'] : $type['duplicate'] = true;
  72. if (strlen(trim($chars)) == 0) {
  73. $type['alphaSmall'] ? $characters .= $alphaSmall : $characters = $characters;
  74. $type['alphaBig'] ? $characters .= $alphaBig : $characters = $characters;
  75. $type['num'] ? $characters .= $num : $characters = $characters;
  76. $type['othr'] ? $characters .= $othr : $characters = $characters;
  77. } else
  78. $characters = str_replace(' ', '', $chars);
  79. if ($type['duplicate'])
  80. for (; $length > 0 && strlen($characters) > 0; $length--) {
  81. $ctr = mt_rand(0, (strlen($characters)) - 1);
  82. $string .= $characters[$ctr];
  83. } else
  84. $string = substr(str_shuffle($characters), 0, $length);
  85. return $string;
  86. }
  87.  
  88. public function randomCode()
  89. {
  90. $tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  91. for ($i = 0; $i < 3; $i++) {
  92. for ($j = 0; $j < 5; $j++) {
  93. $return .= $tokens[rand(0, 35)];
  94. }
  95. if ($i < 2) {
  96. $return .= '';
  97. }
  98. }
  99. return $return;
  100. }
  101.  
  102. public function countUser()
  103. {
  104. return User::find()->count();
  105. }
  106.  
  107. public function templateExcel()
  108. {
  109. return ("@webroot/templates/new.xls");
  110. }
  111.  
  112. /**
  113. * For Custom report translate 0 ke A
  114. */
  115. public function excelChar()
  116. {
  117. return array(
  118. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  119. 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ',
  120. 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ',
  121. 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ',
  122. 'DA', 'DB', 'DC', 'DD', 'DE', 'DF', 'DG', 'DH', 'DI', 'DJ', 'DK', 'DL', 'DM', 'DN', 'DO', 'DP', 'DQ', 'DR', 'DS', 'DT', 'DU', 'DV', 'DW', 'DX', 'DY', 'DZ',
  123. 'EA', 'EB', 'EC', 'EE', 'EE', 'EF', 'EG', 'EH', 'EI', 'EJ', 'EK', 'EL', 'EM', 'EN', 'EO', 'EP', 'EQ', 'ER', 'ES', 'ET', 'EU', 'EV', 'EW', 'EX', 'EY', 'EZ',
  124. );
  125. }
  126.  
  127. public function excelNot()
  128. {
  129. return [
  130. 'userUpdate', 'userCreate', 'createDate', 'updateDate', 'image'
  131. ];
  132. }
  133.  
  134. public function excelParsing($fileExcel)
  135. {
  136. // $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3; /* here i added */
  137. // $cacheEnabled = \PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
  138. // if (!$cacheEnabled) {
  139. // echo "### WARNING - Sqlite3 not enabled ###" . PHP_EOL;
  140. // }
  141. $objPHPExcel = new \PHPExcel();
  142.  
  143. //$fileExcel = Yii::getAlias('@webroot/templates/operator.xls');
  144. $inputFileType = \PHPExcel_IOFactory::identify($fileExcel);
  145.  
  146. $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
  147.  
  148. $objReader->setReadDataOnly(true);
  149.  
  150. /** Load $inputFileName to a PHPExcel Object * */
  151. $objPHPExcel = $objReader->load($fileExcel);
  152.  
  153. $total_sheets = $objPHPExcel->getSheetCount();
  154.  
  155. $allSheetName = $objPHPExcel->getSheetNames();
  156. $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
  157. $highestRow = $objWorksheet->getHighestRow();
  158. $highestColumn = $objWorksheet->getHighestColumn();
  159. $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
  160. for ($row = 1; $row <= $highestRow; ++$row) {
  161. for ($col = 0; $col < $highestColumnIndex; ++$col) {
  162. $value = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  163.  
  164. $arraydata[$row - 1][$col] = $value;
  165. }
  166. }
  167.  
  168. return $arraydata;
  169. }
  170.  
  171. public static function Rp($price)
  172. {
  173. return number_format($price, 0, '.', ',');
  174. }
  175.  
  176. public static function PriceComa($price)
  177. {
  178. return number_format($price, 0, ',', '.');
  179. }
  180.  
  181. public static function usernameOne($id)
  182. {
  183. $user = User::find()->select('username')->where(['id' => $id])->one();
  184. return $user->username;
  185. }
  186.  
  187. public static function settings($companyId = NULL)
  188. {
  189. if (empty($companyId)) {
  190. $companyId = Yii::$app->user->identity->companyId;
  191. }
  192. $model = \app\models\Setting::findOne($companyId);
  193. return $model;
  194. }
  195.  
  196. public static function years()
  197. {
  198. $return = [];
  199. for ($i = 2015; $i <= date(Y) + 1; $i++) {
  200. $return[$i] = $i;
  201. }
  202.  
  203. return $return;
  204. }
  205.  
  206. public static $monthName = [1 => 'January', 'February', 'March', 'April', 'May', 'Juni', 'Juli', 'Augustus', 'September', 'October', 'November', 'December'];
  207.  
  208. public static function months()
  209. {
  210. $return = [];
  211. for ($i = 1; $i <= 12; $i++) {
  212. $r = $i < 10 ? '0' . $i : $i;
  213. $return[$r] = self::$monthName[$i];
  214. }
  215.  
  216. return $return;
  217. }
  218.  
  219. public static function sendSms($to, $msg)
  220. {
  221. if (self::settings()->sms == 1) {
  222. return self::sendSmsTo($to, $msg);
  223. }
  224. }
  225.  
  226. public static function sendSmsTo($to, $msg)
  227. {
  228. $url = 'https://bulksms.vsms.net/eapi/submission/send_sms/2/2.0';
  229. $username = 'sintret';
  230. $password = 'apaajalagi';
  231. $fields = array(
  232. 'username' => $username,
  233. 'password' => $password,
  234. 'message' => $msg,
  235. 'msisdn' => $to
  236. );
  237.  
  238. foreach ($fields as $key => $value) {
  239. $fields_string .= $key . '=' . $value . '&';
  240. }
  241. rtrim($fields_string, '&');
  242. //open connection
  243. $ch = curl_init();
  244. //set the url, number of POST vars, POST data
  245. curl_setopt($ch, CURLOPT_URL, $url);
  246. curl_setopt($ch, CURLOPT_POST, count($fields));
  247. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  248. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  249. //execute post
  250. $result = curl_exec($ch);
  251. //close connection
  252. curl_close($ch);
  253.  
  254. return $result;
  255. }
  256.  
  257. public static function sendSms2($to, $msg)
  258. {
  259. $user = 'sintret';
  260. $password = 'PaVDeWFMIKBbRP';
  261. $url = 'http://api.clickatell.com/http/sendmsg?user=' . $user . '&password=' . $password . '&api_id=3565217&to=' . $to . '&text=' . $msg;
  262.  
  263. $ch = curl_init();
  264.  
  265. // set url
  266. curl_setopt($ch, CURLOPT_URL, $url);
  267.  
  268. //return the transfer as a string
  269. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  270.  
  271. // $output contains the output string
  272. $output = curl_exec($ch);
  273.  
  274. // close curl resource to free up system resources
  275. curl_close($ch);
  276. }
  277.  
  278. public static function cleanFormat($str)
  279. {
  280. $array = ['"', "'"];
  281. return str_replace($array, "", $str);
  282. }
  283.  
  284. public static function cleanCurrencyDot($val)
  285. {
  286. return str_replace(",", "", $val);
  287. }
  288.  
  289. public static function cleanCurrencyComma($val)
  290. {
  291. return str_replace(",", "", $val);
  292. }
  293.  
  294. //this using for work request and work order
  295. public static function dashboards($companyId = NULL)
  296. {
  297.  
  298. if (empty($companyId)) {
  299. $companyId = Yii::$app->user->identity->companyId;
  300. }
  301.  
  302. $view = 'index';
  303. $todolists = NULL;
  304. $histories = NULL;
  305. $settings = \app\models\Setting::findOne($companyId);
  306.  
  307. $userId = Yii::$app->user->id;
  308. $query = \app\models\WorkRequest::find(); //query for todolist
  309. $query->andFilterWhere(['companyId' => $companyId]);
  310. $query->andFilterWhere(['<', 'status', \app\models\WorkRequest::STATUS_REJECTED]);
  311.  
  312. $queryHistory = \app\models\WorkRequest::find(); //query for histories
  313. $queryHistory->andFilterWhere(['>=', 'status', \app\models\WorkRequest::STATUS_REJECTED]);
  314. $queryHistory->andFilterWhere(['companyId' => $companyId]);
  315.  
  316. if (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_WORK_REQUEST) {
  317. $view = 'general';
  318. $query->andFilterWhere(['userCreate' => $userId]);
  319.  
  320. $queryHistory->andFilterWhere(['userCreate' => $userId]);
  321. } elseif (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_WORK_ORDER) {
  322. $view = 'general';
  323. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_PROCESS, 'userCreate' => $userId, 'companyId' => $companyId]);
  324. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_PUBLISH, 'companyId' => $companyId]);
  325. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_SURVEY, 'handledBy' => $userId, 'companyId' => $companyId]);
  326. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_ADJUSTMENT, 'handledBy' => $userId, 'companyId' => $companyId]);
  327. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_APPROVE, 'handledBy' => $userId, 'companyId' => $companyId]);
  328. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_NEED_APPROVAL, 'handledBy' => $userId, 'companyId' => $companyId]);
  329. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_RESOLVED, 'handledBy' => $userId, 'companyId' => $companyId]);
  330. $query->orFilterWhere(['status' => \app\models\WorkRequest::STATUS_PROCESS, 'handledBy' => $userId, 'companyId' => $companyId]);
  331.  
  332. $queryHistory->orFilterWhere(['handledBy' => $userId]);
  333. $queryHistory->orFilterWhere(['userCreate' => $userId]);
  334. } elseif (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_SUPERVISOR) {
  335. $view = 'general';
  336. $approval = $settings->approval;
  337. $query->andFilterWhere(['>=', 'total', $approval]);
  338. $query->andFilterWhere(['status' => \app\models\WorkRequest::STATUS_NEED_APPROVAL, 'companyId' => $companyId]);
  339.  
  340. $queryHistory->andFilterWhere(['>=', 'total', $approval]);
  341. } elseif (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_SPAREPART) {
  342. $view = 'sparepart';
  343. } elseif (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_PLANNER) {
  344. $view = 'general';
  345. } else {
  346. $view = 'index';
  347. }
  348.  
  349. $query->orderBy("id desc");
  350. $todolists = $query->all();
  351. $todolistLocation = \app\models\WorkRequest::getLocations($todolists);
  352.  
  353. $queryHistory->orderBy("id desc");
  354. $histories = $sparePartHistories = $queryHistory->all();
  355. $historiesLocation = \app\models\WorkRequest::getLocations($histories);
  356.  
  357. //for level spare part
  358. if (Yii::$app->user->identity->roleId == \app\models\User::LEVEL_SPAREPART) {
  359. $sparePartTodolist = [];
  360. if ($todolists)
  361. foreach ($todolists as $model) {
  362.  
  363. if ($model->partsCount) {
  364. if (empty($model->userSparePart)) {
  365. $sparePartTodolist[] = $model;
  366. } else if ($model->userSparePart == Yii::$app->user->id) {
  367. $sparePartTodolist[] = $model;
  368. }
  369. }
  370. }
  371.  
  372.  
  373. // if ($histories)
  374. // foreach ($histories as $model) {
  375. // if ($model->partsCount) {
  376. // if (empty($model->userSparePart)) {
  377. // $sparePartHistories[] = $model;
  378. // } else if ($model->userSparePart == Yii::$app->user->id) {
  379. // $sparePartHistories[] = $model;
  380. // }
  381. // }
  382. // }
  383. }
  384.  
  385. return [
  386. 'settings' => $settings,
  387. 'todolists' => $todolists,
  388. 'histories' => $histories,
  389. 'sparePartTodolist' => $sparePartTodolist,
  390. 'sparePartHistories' => $sparePartHistories,
  391. 'locations' => $todolistLocation,
  392. 'view' => $view,
  393. ];
  394. }
  395.  
  396. public static function buildError($errors = [])
  397. {
  398.  
  399. $error = '';
  400. if ($errors) {
  401. foreach ($errors as $k => $v) {
  402. $error .= $v[0] . ' ';
  403. }
  404. Yii::$app->session->setFlash('error', $error);
  405. }
  406. }
  407.  
  408. public static function rcopy($src, $dst)
  409. {
  410. $dir = opendir($src);
  411. @mkdir($dst);
  412. while (false !== ( $file = readdir($dir))) {
  413. if (( $file != '.' ) && ( $file != '..' )) {
  414. if (is_dir($src . '/' . $file)) {
  415. self::rcopy($src . '/' . $file, $dst . '/' . $file);
  416. } else {
  417. copy($src . '/' . $file, $dst . '/' . $file);
  418. }
  419. }
  420. }
  421. closedir($dir);
  422. }
  423.  
  424. public static function resize_image($file, $w, $h, $crop = FALSE)
  425. {
  426. list($width, $height) = getimagesize($file);
  427. $r = $width / $height;
  428. if ($crop) {
  429. if ($width > $height) {
  430. $width = ceil($width - ($width * abs($r - $w / $h)));
  431. } else {
  432. $height = ceil($height - ($height * abs($r - $w / $h)));
  433. }
  434. $newwidth = $w;
  435. $newheight = $h;
  436. } else {
  437. if ($w / $h > $r) {
  438. $newwidth = $h * $r;
  439. $newheight = $h;
  440. } else {
  441. $newheight = $w / $r;
  442. $newwidth = $w;
  443. }
  444. }
  445. $src = imagecreatefromjpeg($file);
  446. $dst = imagecreatetruecolor($newwidth, $newheight);
  447. imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  448.  
  449. return $dst;
  450. }
  451.  
  452. public static function createJson($filename, $json)
  453. {
  454. $fp = fopen($filename, 'w');
  455. fwrite($fp, $json);
  456. fclose($fp);
  457. }
  458.  
  459. /*
  460. * Encrypted and decrypted
  461. */
  462.  
  463. const METHOD = 'andy-256-laser';
  464. CONST HEX2BIN = 'makanapaajayangp';
  465.  
  466. public static function encrypt($encrypt)
  467. {
  468. $mc_key = self::HEX2BIN;
  469. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  470. $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
  471. $encode = base64_encode($passcrypt);
  472. return $encode;
  473. }
  474.  
  475. public static function decrypt($decrypt)
  476. {
  477. $mc_key = self::HEX2BIN;
  478. $decoded = base64_decode($decrypt);
  479. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  480. $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
  481. return $decrypted;
  482. }
  483.  
  484. public static function downloadFile($url, $path)
  485. {
  486. $newfname = $path;
  487. $file = fopen($url, 'rb');
  488. if ($file) {
  489. $newf = fopen($newfname, 'wb');
  490. if ($newf) {
  491. while (!feof($file)) {
  492. fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
  493. }
  494. }
  495. }
  496. if ($file) {
  497. fclose($file);
  498. }
  499. if ($newf) {
  500. fclose($newf);
  501. }
  502. }
  503.  
  504. /* creates a compressed zip file */
  505.  
  506. public static function createZip($files = [], $destination = '', $overwrite = false)
  507. {
  508. //if the zip file already exists and overwrite is false, return false
  509. // if (file_exists($destination) && !$overwrite) {
  510. // return false;
  511. // }
  512. //vars
  513. $valid_files = [];
  514. //if files were passed in...
  515. if (is_array($files)) {
  516. //cycle through each file
  517. foreach ($files as $file) {
  518. //make sure the file exists
  519. if (file_exists($file)) {
  520. $valid_files[] = $file;
  521. }
  522. }
  523. }
  524. //if we have good files...
  525. if (count($valid_files)) {
  526. //create the archive
  527. $zip = new \ZipArchive;
  528. if ($zip->open($destination, $overwrite ? $zip::OVERWRITE : $zip::CREATE) !== true) {
  529. return false;
  530. }
  531. //add the files
  532. foreach ($valid_files as $file) {
  533. $newFile = self::randomString(10) . '.jpg';
  534. $zip->addFile($file, $newFile);
  535. }
  536. //debug
  537. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  538. //close the zip -- done!
  539. $zip->close();
  540.  
  541. //check to make sure the file exists
  542. return file_exists($destination);
  543. } else {
  544. return false;
  545. }
  546. }
  547.  
  548. public static function crawlPage($url, $depth = 5)
  549. {
  550. static $seen = [];
  551. if (isset($seen[$url]) || $depth === 0) {
  552. return;
  553. }
  554.  
  555. $seen[$url] = true;
  556.  
  557. $dom = new \DOMDocument('1.0');
  558. @$dom->loadHTMLFile($url);
  559.  
  560. $anchors = $dom->getElementsByTagName('a');
  561. foreach ($anchors as $element) {
  562. $href = $element->getAttribute('href');
  563. if (0 !== strpos($href, 'http')) {
  564. $path = '/' . ltrim($href, '/');
  565. if (extension_loaded('http')) {
  566. $href = http_build_url($url, array('path' => $path));
  567. } else {
  568. $parts = parse_url($url);
  569. $href = $parts['scheme'] . '://';
  570. if (isset($parts['user']) && isset($parts['pass'])) {
  571. $href .= $parts['user'] . ':' . $parts['pass'] . '@';
  572. }
  573. $href .= $parts['host'];
  574. if (isset($parts['port'])) {
  575. $href .= ':' . $parts['port'];
  576. }
  577. $href .= $path;
  578. }
  579. }
  580. self::crawlPage($href, $depth - 1);
  581. }
  582.  
  583. $return = PHP_EOL . $dom->saveHTML() . PHP_EOL . PHP_EOL;
  584. return $return;
  585. }
  586.  
  587. public static function htmlParser($id)
  588. {
  589.  
  590. $model = \app\models\Cook::findOne($id);
  591. $simple = \keltstr\simplehtmldom\SimpleHTMLDom::str_get_html($model->html);
  592.  
  593. $imageRecipe = $simple->find('div[class=recipe-show__image]', 0)->find("img");
  594. foreach ($imageRecipe as $recipe) {
  595. //$img = $recipe->find("img");
  596. $imagelink = $recipe->src;
  597. }
  598. $title = $simple->find('h1[class=recipe-show__title]', 0)->plaintext;
  599. $description = $simple->find('div[class=recipe-show__story prose]', 0)->plaintext;
  600. $ingredients = $simple->find('div[class=ingredient-list]', 0)->find('li');
  601. $no = 1;
  602. foreach ($ingredients as $ing) {
  603. $ingredient .= $no . '. ' . trim($ing->plaintext) . ' ';
  604. $no++;
  605. }
  606. //echo $ingredient;
  607.  
  608. $steX = $simple->find('ol[class=numbered-list]', 0);
  609. if (!is_null($steX)) {
  610. $steps = $steX->find('li');
  611. if (!is_null($steps)) {
  612. $no = 1;
  613. foreach ($steps as $step) {
  614. $instruction .= $no . '. ' . trim($step->plaintext);
  615. $no++;
  616. }
  617. }
  618. }
  619.  
  620. if ($imagelink) {
  621. $model->title = trim($title);
  622. $model->description = trim($description);
  623. $model->ingredients = trim($ingredient);
  624. $model->instruction = trim($instruction);
  625. $filename = $model->id . '.jpg';
  626. $model->image = '@web/images/cook/' . $model->id . '/' . $model->id . '.jpg';
  627. if (!$model->save()) {
  628. echo $title;
  629. print_r($model->getErrors());
  630. }
  631.  
  632. $uploaddir = \Yii::getAlias(\app\models\Cook::$imagePath) . $model->id . '/';
  633. if (!file_exists($uploaddir)) {
  634. mkdir($uploaddir, 0777, true);
  635. }
  636.  
  637. $uploadfile = $uploaddir . $filename;
  638.  
  639. $uploadThumbdir = $uploaddir . 'thumb/';
  640. if (!file_exists($uploadThumbdir)) {
  641. mkdir($uploadThumbdir, 0777, true);
  642. }
  643.  
  644. $uploadThumb = $uploadThumbdir . $filename;
  645.  
  646. $arrContextOptions = array(
  647. "ssl" => array(
  648. "verify_peer" => false,
  649. "verify_peer_name" => false,
  650. ),
  651. );
  652. $response = file_get_contents($imagelink, false, stream_context_create($arrContextOptions));
  653.  
  654. file_put_contents($uploadfile, $response);
  655. //copy($uploadfile, $uploadThumb);
  656. //self::resize_image($uploadThumb, 120, 120);
  657. Image::getImagine()->open($uploadfile)->thumbnail(new Box(120, 120))->save($uploadThumb,['quality'=>90]);
  658.  
  659. }
  660. }
  661.  
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement