public static function recurse_copy($src, $dst) { $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') { if (is_dir($src . '/' . $file)) { Utils::recurse_copy($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); }
public function copyToUser($to_user_id, $status = Photobook::STATUS_NEW) { $newPhotobookForm = new PhotobookForm(); $newPhotobookForm->user_id = $to_user_id; $newPhotobookForm->name = $this->user_id == $to_user_id ? Yii::t('app', 'Копия ') . $this->name : $this->name; $newPhotobookForm->status = $status; //Photobook::STATUS_NEW; $newPhotobookForm->data = $this->data; $newPhotobookForm->template = $this->template; $newPhotobookForm->photos = $this->photos; $newPhotobookForm->style_id = $this->style_id; $newPhotobookForm->cover_id = $this->cover_id; $newPhotobookForm->title_line_1 = $this->title_line_1; $newPhotobookForm->title_line_2 = $this->title_line_2; $newPhotobookForm->title_line_3 = $this->title_line_3; $newPhotobookForm->title_line_4 = $this->title_line_4; $newPhotobookForm->change_status_at = time(); $newPhotobookForm->view_access_key = null; $newPhotobookForm->invoice_id = null; $newPhotobookForm->photos_zip_hash = null; if (!$newPhotobookForm->save()) { return ['error' => ['msg' => Yii::t('app', 'Не удалось записать в базу данных')]]; } $new_pb_id = $newPhotobookForm->id; $new_user_id = $to_user_id; $src_path = UserUrl::photobook(false, $this->id, $this->user_id); $dst_path = UserUrl::photobook(false, $newPhotobookForm->id, $newPhotobookForm->user_id); try { Utils::recurse_copy($src_path, $dst_path); //fix window_text and tracing $src_window_text = UserUrl::photobookWindowText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($this->id, UserUrl::IMAGE_ORIGINAL, 'png'); $dst_window_text = UserUrl::photobookWindowText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($newPhotobookForm->id, UserUrl::IMAGE_ORIGINAL, 'png'); if (file_exists($src_window_text)) { copy($src_window_text, $dst_window_text); unlink($src_window_text); } $src_tracing = UserUrl::photobookTracingText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($this->id, UserUrl::IMAGE_ORIGINAL, 'png'); $dst_tracing = UserUrl::photobookTracingText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($newPhotobookForm->id, UserUrl::IMAGE_ORIGINAL, 'png'); if (file_exists($src_tracing)) { copy($src_tracing, $dst_tracing); unlink($src_tracing); } return ['response' => ['status' => true, 'id' => $new_pb_id, 'user_id' => $new_user_id, 'redirect' => Url::toRoute(['photobooks/index', 'status' => Photobook::STATUS_NEW])]]; } catch (\Exception $e) { return ['error' => ['msg' => $e->getMessage()]]; } }