CmsTrack.php 22.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 25/12/2015
 * Time: 2:22 CH
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');

class CmsTrack extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
        //
        $this->load->model('track_model');
        $this->load->model('user_model');
        $this->load->model('actionlog_model');
        $this->load->model('crawler_listen_blacklist_model');
    }

    /**
     * Hiển thị ds bài hát
     */
    public function listTrack()
    {
        $this->_data['functionName'] = 'Track';
        $this->_data['action'] = 'listTrack';
        $this->_data['titlePage'] = 'Track';
        $this->_data['loadPage'] = 'backend/cms_track/list_track_view';
        $this->load->view('backend/admin_template_view', $this->_data);
    }

    /**
     * Ajax xử lý lấy ds bài hát
     */
    public function listTrackAjax()
    {
        $data = array();
        $fByName = $this->input->post('fByName');
        $fBySource = $this->input->post('fBySource');
        $fBySinger = $this->input->post('fBySinger');
        $fByComposer = $this->input->post('fByComposer');
        $fByBlacklist = $this->input->post('fByBlacklist');
        $pageId = $this->input->post('pageId');
        //
        $pageId = ($pageId == 0) ? 1 : $pageId;
        //
        $limit = 100;
        $offset = ($pageId - 1) * $limit;
        $data['offset'] = ($pageId - 1) * $limit;
        $totalRecord = $this->track_model->cmsCountAll($fByName, $fBySinger, $fByComposer, $fBySource, $fByBlacklist);
        $data['pagination'] = MyHelper::genPaginationLink($totalRecord, $limit, $pageId);
        $data['listData'] = $this->track_model->cmsGetPagination($limit, $offset, $fByName, $fBySinger, $fByComposer, $fBySource, $fByBlacklist);
        //
        $this->load->view('backend/ajax/cms_track/list_track_view', $data);
    }

    /**
     * Hàm xử lý cập nhật bài hát
     *
     * @param $track_id
     * @param string $id_user
     * @param string $id_artist
     */
    public function editTrack($track_id, $id_user = '', $id_artist = '')
    {
        $this->_data['contest'] = $track = $this->track_model->getById($track_id);
        $track_user = '';
        $this->_data['track_artists'] = $track_artists = '';
        if ($id_user != 0) {
            $track_user = $this->track_model->getByIdUser('track_user', $id_user);
        $this->_data['track_user'] = $track_user;
        }
        if ($id_artist != 0) {
            $track_artists = $this->track_model->getByIdUser('track_artists', $id_artist);
            $this->_data['track_artists'] = $track_artists;
        }
        $this->_data['list_user'] = $list_user = $this->track_model->getListUsers();

        // Kiểm tra trùng giá trị nếu thay đổi ở form

        //Thiết lập validate
        $this->form_validation->set_rules(
            'id',
            'ID',
            'trim'
        );

        $this->form_validation->set_rules(
            'title',
            'Tên bài hát',
            'trim|required|xss_clean'
        );

        $this->form_validation->set_message('required', '<li>Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li>Đã tồn tại.</li>');
        
        // Xử lý form
        if($this->form_validation->run($this) == false) {
            // $this->_data['date_limit'] = $this->input->post('name');
            $this->_data['title'] = $this->input->post('title');
            $this->_data['update_singer'] = $this->input->post('update_singer');
            
            $this->_data['singer'] = $this->input->post('singer');
            $this->_data['artist'] = $this->input->post('artist');
            $this->_data['update_artist'] = $this->input->post('update_artist');
            $this->_data['functionName'] = 'Cập nhật thông tin';
            $this->_data['action'] = 'editTrack';
            $this->_data['titlePage'] = 'Cập nhật thông tin';
            $this->_data['loadPage'] = 'backend/cms_track/edit_track_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            $title = $this->input->post('title');
            $update_singer = $this->input->post('update_singer');
            $singer = ($track_user != '') ? $track_user[0]['user_id']: '';
            $artist = ($track_artists != '') ? $track_artists[0]['user_id']: '';
            $update_artist = $this->input->post('update_artist');
            $file = $_FILES['avatar']['name'];
            $id_singer = ($update_singer > 0) ? $update_singer: $singer;
            $id_artists = ($update_artist > 0) ? $update_artist: $artist;
            ($singer || $id_user) ? $active_singer = 'update': $active_singer = 'insert';
            ($artist || $id_artist) ? $active_artist = 'update': $active_artist = 'insert';
            // Xử lý upload: avatar
            if($file != ''){
                $db_art_path = '';
                $upload_path = 'uploads/tracks/arts';
                $upload_full_path = '/home/aseanvn/public_html/vmusicchart.dcv.vn/' . $upload_path;
                $config['upload_path']          = $upload_full_path;
                $config['file_name'] =  md5($track_id.time());
                $config['allowed_types']        = 'gif|jpg|png|jpeg';
                $config['max_size']             = 20480;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;

                $this->load->library('upload', $config);
                if ($this->upload->do_upload('avatar')) {
                    $upload_data = $this->upload->data();
                    $db_art_path = $upload_path . '/' . $upload_data['file_name'];
                }
                $this->track_model->update($track_id, $title, $db_art_path);
            } else {
                $art = $track[0]['art'];
                $this->track_model->update($track_id, $title, $art);
            }

            $this->track_model->update_track_artists($track_id, $id_artists, $id_artist, $active_artist);
            $this->track_model->update_track_user($track_id, $id_singer, $id_user, $active_singer);

            // Ghi log
            $this->actionlog_model->add('UPDATED', 'Cập nhật bài hát.', 'Actionlog_model', 'tracks', $track_id);

            $this->session->set_flashdata('success', 'Cập nhật bài hát thành công');
            redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
        }
    }

    /**
     * Thêm bài hát mới
     */
    public function addTrack()
    {
        $this->_data['list_user'] = $list_user = $this->track_model->getListUsers();
        // Thiết lập validate
        $this->form_validation->set_rules(
            'id',
            'ID',
            'trim'
        );

        $this->form_validation->set_rules(
            'title',
            'Tên bài hát',
            'trim|required|callback_check_title|xss_clean'
        );

        $this->form_validation->set_rules(
            'singer',
            'Ca sĩ',
            'trim|required|callback_check_default|xss_clean'
        );
        $this->form_validation->set_rules(
            'artist',
            'Sáng tác',
            'trim|required|callback_check_default|xss_clean'
        );
        $this->form_validation->set_rules(
            'nct_id',
            'ID bài hát trên nhạc của tui',
            'callback_check_nct_id|xss_clean'
        );

        $this->form_validation->set_message('required', '<li class="list-unstyled">Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li class="list-unstyled">Đã tồn tại.</li>');
        // Xử lý form
        if($this->form_validation->run($this) == false) {
            $this->_data['title'] = $this->input->post('title');
            $this->_data['singer'] = $this->input->post('singer');
            $this->_data['artist'] = $this->input->post('artist');
            $this->_data['avatar'] = $this->input->post('avatar');
            $this->_data['source'] = $this->input->post('source');
            $this->_data['src_option'] = $this->input->post('src_option');
            $this->_data['link'] = $this->input->post('link');
            $this->_data['nct_id'] = $this->input->post('nct_id');


            $this->_data['functionName'] = 'Tạo mới Bài hát';
            $this->_data['action'] = 'addTrack';
            $this->_data['titlePage'] = 'Tạo mới Bài hát';
            $this->_data['loadPage'] = 'backend/cms_track/add_track_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            $title = $this->input->post('title');
            $singer = $this->input->post('singer');
            $artist = $this->input->post('artist');
            $link = $this->input->post('link');
            $source = $this->input->post('source');
            $nct_id = $this->input->post('nct_id');

            if($title){
                $slug = MyHelper::genSlug($title);
                $dataUpdate = array(
                    'title' => $title,
                    'userid' => $artist,
                    'link' => ($source == 'zing')? $link : '',
                    'link_nct' => ($source == 'nct')? $link : '',
                    'link_keeng' => ($source == 'keeng')? $link : '',
                    'id_nct' => $nct_id,
                    'slug' => $slug,
                    'tag' => 'vmusic',
                    'genre' => 1,
                    'time' => time()
                );
                $insert_id = $this->track_model->add($dataUpdate);
                if ($insert_id) {
                    // Xử lý upload: avatar
                    $db_art_path = '';
                    $upload_path = 'uploads/tracks/arts';
                    $upload_full_path = '/home/aseanvn/public_html/vmusicchart.dcv.vn/' . $upload_path;
                    $config['upload_path']          = $upload_full_path;
                    $config['file_name'] =  md5($insert_id.time());
                    $config['allowed_types']        = 'gif|jpg|png|jpeg';
                    $config['max_size']             = 20480;
                    $config['max_width']            = 1024;
                    $config['max_height']           = 768;

                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload('avatar')) {
                        $upload_data = $this->upload->data();
                        $db_art_path = $upload_path . '/' . $upload_data['file_name'];
                    }
                    $this->track_model->update($insert_id, $title, $db_art_path);
                    $this->track_model->add_singer($singer, $insert_id);
                    $this->track_model->add_artist($artist, $insert_id);
                }
                //
                $this->session->set_flashdata('success', 'Tạo Bài hát thành công');
            } else {
                $this->session->set_flashdata('error', 'Tạo Bài hát không thành công');
            }

            // Ghi log
            $this->actionlog_model->add('ADDED', 'Thêm mới bài hát.', 'Actionlog_model', 'tracks', null);

            redirect(base_url('backend/cmsTrack/listTrack'));
        }
    }

    /**
     * Hàm xử lý thêm ca sĩ cho bài hát
     *
     * @param $track_id
     */
    public function addSinger($track_id, $call_back=null)
    {
        // Thêm đoạn check sự tồn tại của bài hát

        //
        $this->_data['track'] = $contest = $this->track_model->getById($track_id);
        $this->_data['list_user'] = $list_user = $this->track_model->getListUsers();

        $this->form_validation->set_rules(
            'singer',
            'Ca sĩ',
            'trim|required|callback_check_default|xss_clean'
        );

        $this->form_validation->set_message('required', '<li class="list-unstyled">Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li class="list-unstyled">Đã tồn tại.</li>');
        // Xử lý form
        if($this->form_validation->run($this) == false) {
            $this->_data['call_back'] = $call_back;
            $this->_data['singer'] = $this->input->post('singer');

            $this->_data['functionName'] = 'Thêm Người thể hiện vào bài hát';
            $this->_data['action'] = 'addSinger';
            $this->_data['titlePage'] = 'Thêm Người thể hiện vào bài hát';
            $this->_data['loadPage'] = 'backend/cms_track/add_singer_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            $singer = $this->input->post('singer');
            $this->track_model->add_singer($singer,$track_id);
            $this->session->set_flashdata('success', 'Cập nhật Ca sĩ vào bài hát thành công');

            // Ghi log
            $this->actionlog_model->add('ADDED', 'Thêm mới Ca sĩ cho bài hát.', 'Actionlog_model', 'tracks', $track_id);

            // Nếu thêm ca sĩ từ trang home thì quay trở về trang home
            if ($call_back == 'home') {
                redirect(base_url('backend/home/index'));
            }

            redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
        }
    }

    /**
     * Xử lý xóa ca sĩ khỏi bài hát
     *
     * @param $track_id
     * @param $singer_id
     */
    public function removeSinger($track_id, $singer_id)
    {
        // Thêm đoạn check tồn tại bài hát và ca sĩ, ok thì mới gọi xóa

        // Xử lý
        $this->track_model->removeSinger($track_id, $singer_id);

        // Ghi log
        $this->actionlog_model->add('DELETED', 'Xóa Ca sĩ khỏi bài hát.', 'Actionlog_model', 'tracks', $track_id);

        $this->session->set_flashdata('success', 'Xóa Ca sĩ khỏi bài hát thành công');
        redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
    }

    /**
     * Hàm xử lý thêm Nhạc sĩ cho bài hát
     *
     * @param $track_id
     */
    public function addComposer($track_id, $call_back=null)
    {
        // Thêm đoạn check sự tồn tại của bài hát

        // Xử lý
        $this->_data['track'] = $contest = $this->track_model->getById($track_id);
        $this->_data['list_user'] = $list_user = $this->track_model->getListUsers();

        $this->form_validation->set_rules(
            'composer',
            'Nhạc sĩ',
            'trim|required|callback_check_default|xss_clean'
        );

        $this->form_validation->set_message('required', '<li class="list-unstyled">Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li class="list-unstyled">Đã tồn tại.</li>');
        // Xử lý form
        if($this->form_validation->run($this) == false) {
            $this->_data['call_back'] = $call_back;
            $this->_data['composer'] = $this->input->post('composer');

            $this->_data['functionName'] = 'Thêm Người thể hiện vào bài hát';
            $this->_data['action'] = 'addComposer';
            $this->_data['titlePage'] = 'Thêm Người thể hiện vào bài hát';
            $this->_data['loadPage'] = 'backend/cms_track/add_composer_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            $composer = $this->input->post('composer');
            $this->track_model->add_artist($composer, $track_id);
            $this->session->set_flashdata('success', 'Cập nhật Nhạc sĩ vào bài hát thành công');

            // Ghi log
            $this->actionlog_model->add('ADDED', 'Thêm Nhạc sĩ cho bài hát.', 'Actionlog_model', 'tracks', $track_id);

            // Nếu thêm nhạc sĩ từ trang home thì quay trở về trang home
            if ($call_back == 'home') {
                redirect(base_url('backend/home/index'));
            }

            redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
        }
    }

    public function updateTrackImage($track_id, $call_back=null)
    {
        // Thêm đoạn check sự tồn tại của bài hát

        //
        $this->_data['track'] = $contest = $this->track_model->getById($track_id);

        $this->form_validation->set_rules(
            'track_image',
            'Ảnh bài hát',
            ''
        );

        $this->form_validation->set_message('required', '<li class="list-unstyled">Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li class="list-unstyled">Đã tồn tại.</li>');

        // Xử lý form
        if($this->form_validation->run($this) == false) {
            $this->_data['track_image'] = $this->input->post('track_image');
            //
            $this->_data['functionName'] = 'Cập nhật ảnh bài hát';
            $this->_data['action'] = __FUNCTION__;
            $this->_data['titlePage'] = 'Cập nhật ảnh bài hát';
            $this->_data['loadPage'] = 'backend/cms_track/update_track_image_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            if (empty($_FILES['track_image']['name'])) {
                $this->session->set_flashdata('error', 'Cập nhật ảnh bài hát không thành công');
                redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
            }

            $update = $this->privateUpdateTrackArt($track_id, 'track_image', true);

            if ($update) {
                $this->session->set_flashdata('success', 'Cập nhật ảnh bài hát thành công');
            } else {
                $this->session->set_flashdata('error', 'Cập nhật ảnh bài hát không thành công');
            }

            // Ghi log
            $this->actionlog_model->add('UPDATED', 'Cập nhật ảnh cho bài hát.', 'Actionlog_model', 'tracks', $track_id);

            // Nếu thêm ca sĩ từ trang home thì quay trở về trang home
            if ($call_back == 'home') {
                redirect(base_url('backend/home/index'));
            }

            redirect(base_url('backend/cmsTrack/showTrack/'.$track_id));
        }
    }

    /**
     * Xem chi tiết bài hát
     *
     * @param $track_id
     */
    public function showTrack($track_id)
    {
        // Thêm đoạn check sự tồn tại của bài hát

        //
        $this->_data['track']= $this->track_model->getById($track_id);
        $this->_data['blacklist']= $this->crawler_listen_blacklist_model->getSrcByTrackId($track_id, null);
        $this->_data['singers']= $this->track_model->getSingerLítByTrackId($track_id);
        //
        $this->_data['functionName'] = 'Thông tin Bài Hát';
        $this->_data['action'] = 'showTrack';
        $this->_data['titlePage'] = 'Thông tin Bài Hát';
        $this->_data['loadPage'] = 'backend/cms_track/show_track_view';
        $this->load->view('backend/admin_template_view', $this->_data);
    }

    /**
     * Tên bài hát tồn tại hay chưa
     *
     * @param $title
     * @return bool
     */
    public function check_title($title)
    {
        if ($this->input->post('id')) {
            $id = $this->input->post('id');
        }
        else {
            $id = '';
        }
        $result = $this->track_model->getByTitle($title, $id);
        if($result) {
            $this->form_validation->set_message('check_title', '<li class="list-unstyled">Bài hát đã tồn tại.</li>');
           return false;
        }else{
            return true;
        }
    }

    /**
     * Kiểm tra đã chọn ca sĩ/nhạc sĩ
     *
     * @param $value
     * @return bool
     */
    public function check_default($value)
    {
        if ($value == '0') {
            $this->form_validation->set_message('check_default', '<li class="list-unstyled">Vui lòng chọn ca sĩ hoặc sáng tác.</li>');
           return false;
        } else
        return true;
    }

    /**
     * Kiểm tra id_nct khác null
     *
     * @param $nct_id
     * @return bool
     */
    public function check_nct_id($nct_id)
    {
        $source = $this->input->post('source');
        if($nct_id == '' && $source == 'nct') {
            $this->form_validation->set_message('check_nct_id', '<li class="list-unstyled">Vui lòng nhập ID bài hát trên NCT.</li>');
           return false;
        } else
        return true;
    }

    /**
     * Hàm hỗ trợ upload file ảnh bài hát
     *
     * @param $track_id
     * @param $track_image_field
     * @param bool $is_same_src
     * @return bool
     */
    private function privateUpdateTrackArt($track_id, $track_image_field, $is_same_src=false)
    {
        // Xử lý upload: avatar
        $db_art_path = '';
        $upload_path = 'uploads/tracks/arts';
        if ($is_same_src) {
            $art_type = 'same';
            $upload_full_path = $upload_path;
        } else {
            $art_type = 'other';
            $upload_full_path = '/home/aseanvn/public_html/vmusicchart.dcv.vn/' . $upload_path;
        }

        $config['upload_path']          = $upload_full_path;
        $config['file_name'] =  md5($track_id.time());
        $config['allowed_types']        = 'gif|jpg|png|jpeg';
        $config['max_size']             = 20480;
        $config['max_width']            = 500;
        $config['max_height']           = 500;

        $this->load->library('upload', $config);

        if ($this->upload->do_upload($track_image_field)) {
            $upload_data = $this->upload->data();
            $db_art_path = $upload_path . '/' . $upload_data['file_name'];
            $this->track_model->update_art($track_id, $db_art_path, $art_type);

            return true;
        }

        return false;
    }

    /**
     * Cập nhật tích chọn không lấy lượt nghe ở trang nhạc
     */
    public function updateBlacklistByAjax()
    {
        $track_id = $this->input->post('track_id');
        $title = $this->input->post('title');
        $src = $this->input->post('src');
        $selected = $this->input->post('selected');
        //
        $result = $this->crawler_listen_blacklist_model->checkAndUpdate($track_id, $title, $src, $selected);
        //
        if ($result) {
            echo json_encode([
                'code' => 200,
                'message' => 'Cập nhật thành công',
                'data' => null
            ]);
        } else {
            echo json_encode([
                'code' => 400,
                'message' => 'Cập nhật không thành công',
                'data' => null
            ]);
        }
    }

}