Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.93 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @description api react for issue log
  5.  * @author Ilya Khalizov, Ilia Kovalenko
  6.  */
  7.  
  8. class issueLogController extends apiController
  9. {
  10.     /**
  11.      * equels issue id
  12.      */
  13.     private $_page_id;
  14.  
  15.     public function __construct()
  16.     {
  17.         parent::__construct();
  18.         require_once ROOT_DIR . '/plugins/function.imageurl.php';
  19.         if ($this->_input['data']) {
  20.             $data = json_decode($this->_input['data']);
  21.             $this->_page_id = (int)$data->page_id;
  22.         } else {
  23.             $this->_page_id = (int)$this->_input['page_id'];
  24.         }
  25.     }
  26.  
  27.     /**
  28.      * @description get issue's log
  29.      * @return object
  30.      */
  31.     public function listAction()
  32.     {
  33.         if ($this->_input['page_id'] > 0) {
  34.             $this->_result = IssueLog::findById($this->_input['page_id']);
  35.         } else {
  36.             $this->_result = IssueLog::findByFilterParams($this->_input);
  37.         }
  38.  
  39.         $this->_result['allow_change_filds'] = IssueLog::checkEditAccess($this->_input['page_id']);
  40.         $this->_result['allow_change_filds_creator'] = IssueLog::getAllowedToChangeIssueStatusIds(
  41.             $this->_loggedUser->get('username')
  42.         );
  43.         $this->_result['admin'] = $this->_loggedUser->get('admin');
  44.         $this->output();
  45.     }
  46.  
  47.     /**
  48.      * @description get issue's log on short format
  49.      * @return object
  50.      */
  51.     public function shortListAction()
  52.     {
  53.         $this->_result = IssueLog::findByFilterParamsShort($this->_input);
  54.         $this->_result['allow_change_filds'] = IssueLog::checkEditAccess($this->_input['page_id']);
  55.         $this->_result['allow_change_filds_creator'] = IssueLog::getAllowedToChangeIssueStatusIds(
  56.             $this->_loggedUser->get('username')
  57.         );
  58.         $this->_result['admin'] = $this->_loggedUser->get('admin');
  59.         $this->output();
  60.     }
  61.  
  62.     /**
  63.      * @description change responsible person
  64.      * @return json
  65.      *
  66.      * @var $this
  67.      * @var $query
  68.      * @var $page_id
  69.      * @var $resp_person
  70.      * @var $user
  71.      *
  72.      */
  73.     public function changeResponsibleAction()
  74.     {
  75.         $status = false;
  76.         if ($this->_input['resp_person']) {
  77.             $resp_person = mysql_real_escape_string($this->_input['resp_person']);
  78.             $issue = new IssueLog($this->_page_id);
  79.             $issue->set('resp_username', $resp_person);
  80.             $issue->update();
  81.             $user = $this->_dbr->getAssoc("SELECT username, `name` FROM users WHERE username = '$resp_person'");
  82.             $this->_result['resp_username'] = $user;
  83.             $this->_result['issuelog_id'] = $this->_page_id;
  84.             $this->sendAndLogEmail('responsible');
  85.             $status = true;
  86.         }
  87.         $this->_result['success'] = $status;
  88.         $this->output();
  89.     }
  90.  
  91.     /**
  92.      * @description change solving responsible person
  93.      * @return json
  94.      *
  95.      * @var $this
  96.      * @var $query
  97.      * @var $page_id
  98.      * @var $resp_person
  99.      * @var $user
  100.      *
  101.      */
  102.     public function changeSolvingResponsibleAction()
  103.     {
  104.         $status = false;
  105.         if (IssueLog::checkEditAccess($this->_input['page_id'])) {
  106.             if ($this->_input['solving_resp_person']) {
  107.                 $solving_resp_person = mysql_real_escape_string($this->_input['solving_resp_person']);
  108.                 $issue = new IssueLog($this->_page_id);
  109.                 $issue->set('solving_resp_username', $solving_resp_person);
  110.                 $issue->update();
  111.                 $user = $this->_dbr->getAssoc("SELECT username, `name` FROM users WHERE username = '$solving_resp_person'");
  112.                 $this->_result['resp_username'] = $user;
  113.                 $this->_result['issuelog_id'] = $this->_page_id;
  114.                 $this->sendAndLogEmail('solving');
  115.                 $status = true;
  116.             }
  117.         }
  118.         $this->_result['success'] = $status;
  119.         $this->output();
  120.     }
  121.  
  122.     /**
  123.      * @description change department
  124.      * @return json
  125.      *
  126.      * @var $this
  127.      * @var $query
  128.      * @var $departament
  129.      *
  130.      */
  131.     public function changeDepartmentAction()
  132.     {
  133.         $status = false;
  134.         if (IssueLog::checkEditAccess($this->_input['page_id']))
  135.         {
  136.             if (isset($this->_input['department']))
  137.             {
  138.                 $departments = $this->_input['department'];
  139.                 $issue = new IssueLog($this->_page_id);
  140.                 $this->_result['department_ids'] = $issue->saveDepartments($departments);
  141.                 $status = true;
  142.             }
  143.         }
  144.         $this->_result['success'] = $status;
  145.         $this->output();
  146.     }
  147.  
  148.     /**
  149.      * @description change issue description
  150.      * @return json
  151.      *
  152.      * @var $this
  153.      * @var $query
  154.      * @var $issueDescription
  155.      *
  156.      */
  157.     public function changeIssueDescriptionAction()
  158.     {
  159.         $status = false;
  160.         if (IssueLog::checkEditAccess($this->_input['page_id'])) {
  161.             $issueDescription = mysql_real_escape_string($this->_input['change_issue_description']);
  162.             $issue = new IssueLog($this->_page_id);
  163.             $issue->set('issue', $issueDescription);
  164.             $issue->update();
  165.             $status = true;
  166.         }
  167.         $this->_result['success'] = $status;
  168.         $this->output();
  169.     }
  170.  
  171.     /**
  172.      * set due date
  173.      * @var $due_date
  174.      * @var $this
  175.      */
  176.     public function setDueDateAction()
  177.     {
  178.         $due_date = mysql_real_escape_string($this->_input['due_date']);
  179.         $issue = new IssueLog($this->_page_id);
  180.         $issue->set('due_date', $due_date);
  181.         $issue->update();
  182.         $this->_result['success'] = true;
  183.         $this->output();
  184.     }
  185.  
  186.     /**
  187.      * delete due date
  188.      * @var $this
  189.      */
  190.     public function deleteDueDateAction()
  191.     {
  192.         $issue = new IssueLog($this->_page_id);
  193.         $issue->set('due_date', null);
  194.         $issue->update();
  195.         $this->_result['success'] = true;
  196.         $this->output();
  197.     }
  198.  
  199.     /**
  200.      * @description change type
  201.      * @return json
  202.      *
  203.      * @var $this
  204.      * @var $query
  205.      * @var $issue_type_id
  206.      *
  207.      */
  208.     public function changeIssueTypeAction()
  209.     {
  210.         //checkEditAccess - https://trello.com/c/cfqp58X0/4716-natalia-g-issue-list-small-change
  211.  
  212.         //if (IssueLog::checkEditAccess($this->_input['page_id'])) {
  213.             $issue_type_id = $this->_input['issue_type_id'];
  214.             $issue = new IssueLog($this->_page_id);
  215.             $issue->replaceIssueTypes($issue_type_id);
  216.             $this->_result['success'] = true;
  217.             $this->output();
  218.         //}
  219.     }
  220.  
  221.     /**
  222.      * @description save the issue images, files and videos
  223.      * @var $images
  224.      * @var $imgs_qty
  225.      * @var $file_name
  226.      * @var $file_extension
  227.      * @var $type
  228.      * @var $pic
  229.      * @var $md5
  230.      * @var $file
  231.      * @var $mime_type
  232.      */
  233.     public function saveImagesAction()
  234.     {
  235.         $images = $_FILES['imgs'] ? $_FILES['imgs'] : [];
  236.         if ($images) {
  237.             $imgs_qty = count($images['tmp_name'])-1;
  238.             if ($imgs_qty >= 0) {
  239.                 for(; $imgs_qty >= 0; $imgs_qty--) {
  240.                     $tmp_name = $images['tmp_name'][$imgs_qty];
  241.                     $file_name = $images['name'][$imgs_qty];
  242.                     $mime_type = $images['type'][$imgs_qty];
  243.                     $issue = new IssueLog($this->_page_id);
  244.                     $issue->addFile($file_name, $tmp_name, $mime_type);
  245.                 }
  246.             }
  247.         }
  248.         $this->getIssueImagesAction();
  249.     }
  250.  
  251.     /**
  252.      * @description delete the issue images and files
  253.      */
  254.     public function deleteImagesAction()
  255.     {
  256.         $images = $this->_input['images_delete'] ? $this->_input['images_delete'] : [];
  257.         foreach ($images as $image) {
  258.             $image_hash = $this->_dbr->getOne("SELECT `hash` FROM issue_pic WHERE id = $image");
  259.             (new UploadFiles())->delete($image_hash);
  260.            
  261. //            $image_path = set_file_path($image_hash);
  262. //            if (is_file($image_path)) {
  263. //                unlink($image_path);
  264. //            }
  265.             $this->_db->query("DELETE FROM issue_pic WHERE id = $image");
  266.         }
  267.         $this->getIssueImagesAction();
  268.     }
  269.  
  270.     /**
  271.      * @description get issue images
  272.      */
  273.     public function getIssueImagesAction()
  274.     {
  275.         $issue = new IssueLog($this->_page_id);
  276.         $this->_result['images'] = $issue->getFiles('pic');
  277.         $this->_result['files'] = $issue->getFiles('doc');
  278.         $this->_result['videos'] = $issue->getFiles('video');
  279.         $this->output();
  280.     }
  281.  
  282.     /**
  283.      * get issue types
  284.      */
  285.     public function getIssueTypesAction() {
  286.         $query = "SELECT `name`, inactive, id FROM issue_type";
  287.         $result = $this->_dbr->getAll($query);
  288.         $this->_result['issue_types'] = $result;
  289.         $default_issue_type = \Config::get('default_issue_type');
  290.         $this->_result['default_issue_type'] = $default_issue_type;
  291.         $this->output();
  292.     }
  293.  
  294.     /**
  295.      * set issue types
  296.      * @return object
  297.      */
  298.     public function setIssueTypesAction() {
  299.         if ($this->_input['name'] && !$this->_input['type_id']) {
  300.             $name = mysql_real_escape_string($this->_input['name']);
  301.             $this->_db->query("INSERT INTO issue_type (`name`) VALUES ('" . $name . "')");
  302.         }
  303.         if ($this->_input['name'] && $this->_input['type_id']) {
  304.             $name = mysql_real_escape_string($this->_input['name']);
  305.             $this->_db->query("UPDATE issue_type SET `name` = '$name' WHERE id = " . $this->_input['type_id']);
  306.         }
  307.         if ($this->_input['default']) {
  308.             \Config::set('default_issue_type', (int)$this->_input['default']);
  309.         }
  310.         if ($this->_input['inactive'] >= 0 && $this->_input['type_id']) {
  311.             $this->_db->query("UPDATE issue_type SET inactive = " . (int)$this->_input['inactive'] . " WHERE id = " . $this->_input['type_id']);
  312.         }
  313.         if ($this->_input['inspection'] >= 0 && $this->_input['type_id']) {
  314.             $this->_db->query("UPDATE issue_type SET inspection = " . (int)$this->_input['inspection'] . " WHERE id = " . $this->_input['type_id']);
  315.         }
  316.         $this->_result['success'] = true;
  317.         $this->getIssueTypesAction();
  318.     }
  319.  
  320.     /**
  321.      * @description send email and log it when choose new responsible person
  322.      */
  323.     private function sendAndLogEmail($type)
  324.     {
  325.         if ($type == 'responsible') {
  326.             $person = $this->_input['resp_person'];
  327.             $field = 'resp_username';
  328.         }
  329.         if ($type == 'solving') {
  330.             $person = $this->_input['solving_resp_person'];
  331.             $field = 'solving_resp_username';
  332.         }
  333.         global $siteURL;
  334.         $current_respuser= $this->_db->getOne("select " . $field . " from issuelog where id=" . $this->_page_id);
  335.         $newuser_email = $this->_db->getOne("select email from users where username='" . $person . "'");
  336.         if ($current_respuser != $resp_person) {
  337.             if (strlen($person)) {
  338.                 $ret = new stdClass;
  339.                 $ret->from = $this->_loggedUser->get('email');
  340.                 $ret->from_name = $this->_loggedUser->get('name');
  341.                 $user = new User($this->_db, $this->_dbr, $person);
  342.                 $ret->email_invoice = $newuser_email;
  343.                 $ret->saved_id = $this->_page_id;
  344.                 $ret->auction_number = $this->_page_id;
  345.                 $ret->txnid = 'issuelog';
  346.                 $ret->url = $siteURL . "react/logs/issue_logs/$this->_page_id/";
  347.                 standardEmail($this->_db, $this->_dbr, $ret, 'new_responsible_issue');
  348.             }
  349.         }
  350.     }
  351.  
  352.  
  353.     /**
  354.      * print checked issues
  355.      * @var $issue_ids
  356.      * @var $this
  357.      * @var $dompdf
  358.      * @var $smarty
  359.      * @var $content
  360.      * @var $html
  361.      */
  362.     public function issuesPrintAction()
  363.     {
  364.         global $smarty;
  365.         $issue_ids = $this->_input['issue_ids'];
  366.         $issue_ids = implode(',', $issue_ids);
  367.         $issues = $this->_dbr->getAll("
  368.            SELECT
  369.                GROUP_CONCAT(issue_type.`name` separator '<br>') issue_type
  370.                , issuelog.issue
  371.                , (
  372.                    SELECT Updated
  373.                    FROM total_log
  374.                    WHERE
  375.                        TableID = issuelog.id
  376.                        AND " . get_table_name('issuelog') . "
  377.                        AND " . get_field_name('id') . "
  378.                    ) AS added_time
  379.                , (
  380.                    SELECT users.`name`
  381.                    FROM total_log
  382.                    JOIN users ON users.id = total_log.username_id
  383.                    WHERE
  384.                        TableID = issuelog.id
  385.                        AND " . get_table_name('issuelog') . "
  386.                        AND " . get_field_name('id') . "
  387.                    ) AS added_person
  388.                , users.`name` solving_person
  389.                , empd.`name` departament
  390.                , issuelog.obj,
  391.                    CASE obj
  392.                        WHEN 'supplier' THEN CONCAT('Supplier: ', issuelog.obj_id)
  393.                        WHEN 'auction' THEN CONCAT('Auftrag: ',
  394.                            IF(auction.auction_number, auction.auction_number, issuelog.obj_id),
  395.                            IF(auction.txnid, CONCAT('/',auction.txnid), ''))
  396.                        WHEN 'rma' THEN CONCAT('Ticket: ', issuelog.obj_id)
  397.                        WHEN 'ww_order' THEN CONCAT('WWO: ', issuelog.obj_id)
  398.                        WHEN 'op_order' THEN CONCAT('OP Order: ', issuelog.obj_id)
  399.                        WHEN 'route' THEN CONCAT('Route: ', issuelog.obj_id)
  400.                        WHEN 'manual' THEN 'Manual'
  401.                        WHEN 'rating' CONCAT(THEN 'Rating: ', issuelog.obj_id)
  402.                        WHEN 'insurance' THEN CONCAT('Insurance: ', issuelog.obj_id)
  403.                        WHEN 'shippingInvoiceResults' THEN CONCAT('Shipping invoice: ', issuelog.obj_id)
  404.                        WHEN 'monitoredShippingPrices' THEN CONCAT('Monitored shipping price: ', issuelog.obj_id)
  405.                    END AS where_did
  406.            FROM issuelog
  407.            LEFT JOIN emp_department empd ON empd.id = issuelog.department_id
  408.            LEFT JOIN users ON users.username = issuelog.resp_username
  409.            LEFT JOIN users users_solving ON users_solving.username = issuelog.solving_resp_username
  410.            LEFT JOIN issue_type ON issue_type.id = issuelog.type_id
  411.            LEFT JOIN auction ON auction.id = issuelog.obj_id AND issuelog.obj = 'auction'
  412.            LEFT JOIN rma ON rma.rma_id = issuelog.obj_id AND issuelog.obj = 'rma'
  413.            LEFT JOIN auction auction_rma ON rma.auction_number = auction_rma.auction_number AND rma.txnid = auction_rma.txnid
  414.            LEFT JOIN orders ON auction.auction_number = orders.auction_number and auction.txnid = orders.txnid
  415.            LEFT JOIN orders orders_rma ON rma.auction_number = orders_rma.auction_number and rma.txnid = orders_rma.txnid
  416.            LEFT JOIN auction_par_varchar ON auction.auction_number = auction_par_varchar.auction_number AND auction.txnid = auction_par_varchar.txnid AND auction_par_varchar.`key` = 'country_shipping'
  417.            LEFT JOIN auction_par_varchar apv_rma ON rma.auction_number = apv_rma.auction_number AND rma.txnid = apv_rma.txnid AND apv_rma.`key` = 'country_shipping'
  418.            LEFT JOIN total_log tl ON
  419.                tl.TableID = issuelog.id
  420.                AND " . get_table_name('issuelog', 'tl') . "
  421.                AND " . get_field_name('id', 'tl') . "
  422.            LEFT JOIN issuelog_type ON issuelog_type.issuelog_id = issuelog.id
  423.            WHERE issuelog.id IN ($issue_ids)
  424.                GROUP BY added_time
  425.                ORDER BY added_time DESC
  426.        ");
  427.  
  428.         if ($issues) {
  429.             $smarty->assign('issues', $issues);
  430.             $html = $smarty->fetch('issues.tpl');
  431.             require_once("dompdf/dompdf_config.inc.php");
  432.             $dompdf = new DOMPDF();
  433.             $dompdf->set_paper('A4', 'landscape');
  434.             $dompdf->load_html($html);
  435.             $dompdf->render();
  436.             $content = $dompdf->output();
  437.             header("Content-type: application/pdf");
  438.             header("Content-disposition: inline; filename=issues.pdf");
  439.             echo $content;
  440.         }
  441.     }
  442.  
  443.  
  444.     /**
  445.      * @description Saving filter settings
  446.      */
  447.     public function saveFilterSettingsAction()
  448.     {
  449.         $filter = [];
  450.         $success = false;
  451.         $id = 0;
  452.         $options = [];
  453.  
  454.         $filter_id = intval($this->_input['filter_id']);
  455.  
  456.         if (isset($this->_input['settingName']))
  457.         {
  458.             $title = mysql_real_escape_string(trim($this->_input['settingName']));
  459.             //$title_exists = $this->_db->getOne("SELECT title FROM saved_filters WHERE title = '".$title."'");
  460.  
  461.             $filter_fields = [  'seller',
  462.                 'source_seller',
  463.                 'shipping_country',
  464.                 'shipping_method',
  465.                 'warehouse_shipped_from',
  466.                 'by_comment',
  467.                 'date_from',
  468.                 'date_to',
  469.                 'show_with_inactive',
  470.                 'status',
  471.                 'where_did',
  472.                 'issue_type',
  473.                 'added_person',
  474.                 'department_id',
  475.                 'resp_username',
  476.                 'solving_resp_username',
  477.                 'recurring',
  478.                 'supplier_id',
  479.             ];
  480.  
  481.             $input_fields = $this->_input['filters'];
  482.  
  483.             foreach ( $filter_fields as $filter_field ) {
  484.                 if (isset($input_fields[$filter_field])) {
  485.                     $filter[$filter_field] = mysql_real_escape_string($input_fields[$filter_field]);
  486.                 }
  487.             }
  488.             $filter_json = json_encode($filter);
  489.  
  490.             $id = IssueLog::saveFilterSettings($filter_id, $title, $filter_json);
  491.  
  492.             /*
  493.             if ($filter_id > 0) {
  494.                 // update filter
  495.                 $this->_db->query("UPDATE saved_filters
  496.                     SET `title` = '" . $title . "', `filter_set` = '" . $filter_json . "', `page` = 'issue_logs'
  497.                     WHERE id = " . $filter_id . "
  498.                 ");
  499.             } else {
  500.                 // save new filter
  501.                 $this->_db->query("INSERT INTO saved_filters (`title`, `filter_set`, `page` )
  502.                 VALUES ('" . $title . "', '" . $filter_json . "', 'issue_logs')");
  503.             }
  504.             $id = $this->_db->getOne('SELECT LAST_INSERT_ID()');
  505.             */
  506.  
  507.             if ($id > 0) {
  508.                 $q = "SELECT saved_filters.id, saved_filters.title, saved_filters.filter_set
  509.                       FROM saved_filters
  510.                      WHERE saved_filters.id = " . $id;
  511.                 $result = $this->_dbr->getAssoc($q);
  512.                 foreach ($result as &$item) {
  513.                     $item['filter_set'] = json_decode($item['filter_set']);
  514.                 }
  515.                 $success = true;
  516.             }
  517.         }
  518.  
  519.         if ($success && $id > 0) {
  520.             $this->_result['options']['issue_filter_settings'] = $result;
  521.         }
  522.  
  523.         $this->_result['success'] = $success;
  524.         $this->output();
  525.     }
  526.  
  527.     /**
  528.      * @description Deleting filter settings
  529.      */
  530.     public function deleteFilterSettingsAction()
  531.     {
  532.         $success = false;
  533.         $user_id = $this->_loggedUser->get('id');
  534.         $filter_id = intval($this->_input['filter_id']);
  535.         if ($filter_id > 0 && $user_id > 0) {
  536.             $q = "
  537.                DELETE saved_filters
  538.                FROM saved_filters
  539.                  JOIN total_log ON  " . get_table_name('saved_filters') . " AND
  540.                                     `total_log`.username_id = " . $user_id . " AND
  541.                                     `total_log`.TableID = saved_filters.id
  542.                WHERE saved_filters.id = " . $filter_id . "
  543.            ";
  544.             $this->_db->query($q);
  545.             $success = true;
  546.         }
  547.         $this->_result['filter_id'] = $filter_id;
  548.         $this->_result['success'] = $success;
  549.         $this->output();
  550.     }
  551.  
  552.     /**
  553.      * @description Adding/editing checkpoint
  554.      *
  555.      * @var $checkpoint_id
  556.      * @var $issue_id
  557.      * @var $description
  558.      * @var $ordering
  559.      * @var $done
  560.      */
  561.     public function saveCheckpointAction()
  562.     {
  563.         $success = false;
  564.         $checkpoint_info = [];
  565.         $issue_id = intval($this->_input['issue_id']);
  566.  
  567.         if ($issue_id > 0)
  568.         {
  569.             $issue = new IssueLog($issue_id);
  570.             $checkpoint_id = $issue->saveCheckpoint([
  571.                 'id' => intval($this->_input['checkpoint_id']),
  572.                 'issuelog_id' => $issue_id,
  573.                 'description' => mysql_real_escape_string($this->_input['description']),
  574.                 'ordering' => ($this->_input['ordering']) ? intval($this->_input['ordering']) : 0,
  575.                 'done' => ($this->_input['done'] > 0) ? 1 : 0
  576.             ]);
  577.  
  578.             if ($checkpoint_id > 0)
  579.             {
  580.                 $checkpoint_info = $issue->getCheckpoint($checkpoint_id);
  581.                 $success = true;
  582.             }
  583.         }
  584.  
  585.         $this->_result['checkpoint'] = $checkpoint_info;
  586.         $this->_result['success'] = $success;
  587.         $this->output();
  588.     }
  589.  
  590.  
  591.     /**
  592.      * @description Adding/editing checkpoints
  593.      */
  594.     public function saveCheckpointsAction()
  595.     {
  596.         $success = false;
  597.         $checkpoints_info = [];
  598.         $issue_id = intval($this->_input['issue_id']);
  599.         $checkpoints = $this->_input['checkpoints'];
  600.  
  601.         if (is_array($checkpoints) && $issue_id > 0)
  602.         {
  603.             $issue = new IssueLog($issue_id);
  604.             foreach ($checkpoints as $checkpoint)
  605.             {
  606.                 $checkpoint_id = $issue->saveCheckpoint([
  607.                     'id' => intval($checkpoint['checkpoint_id']),
  608.                     'issuelog_id' => $issue_id,
  609.                     'description' => mysql_real_escape_string($checkpoint['description']),
  610.                     'ordering' => (isset($checkpoint['ordering'])) ? intval($checkpoint['ordering']) : 0,
  611.                     'done' => ($checkpoint['done'] > 0) ? 1 : 0
  612.                 ]);
  613.                 if ($checkpoint_id > 0)
  614.                 {
  615.                     $checkpoints_info[] = $issue->getCheckpoint($checkpoint_id);
  616.                     $success = true;
  617.                 }
  618.             }
  619.         }
  620.  
  621.         $this->_result['checkpoints'] = $checkpoints_info;
  622.         $this->_result['success'] = $success;
  623.         $this->output();
  624.     }
  625.  
  626.  
  627.     /**
  628.      * @description Removing checkpoint
  629.      *
  630.      * @var $checkpoint_id
  631.      * @var $issue_id
  632.      *
  633.      */
  634.     public function removeCheckpointAction()
  635.     {
  636.         $issue_id = intval($this->_input['issue_id']);
  637.         $checkpoint_id = intval($this->_input['checkpoint_id']);
  638.  
  639.         if ($issue_id > 0 && $checkpoint_id > 0)
  640.         {
  641.             $q = "DELETE FROM issue_checkpoint WHERE
  642.                    issue_checkpoint.id = " . $checkpoint_id . " AND
  643.                    issue_checkpoint.issuelog_id = " . $issue_id;
  644.             $this->_db->query($q);
  645.         }
  646.  
  647.         $this->checklistAction();
  648.     }
  649.  
  650.     /**
  651.      * @description Getting checklist
  652.      *
  653.      * @var $issue_id
  654.      */
  655.     public function checklistAction()
  656.     {
  657.         $success = false;
  658.         $issue_id = intval($this->_input['issue_id']);
  659.         $checklist = array();
  660.         if ($issue_id > 0) {
  661.             $checklist = IssueLog::getChecklist($issue_id);
  662.             $success = true;
  663.         }
  664.         $this->_result['checklist'] = IssueLog::checklistReorder($issue_id, $checklist);
  665.         $this->_result['success'] = $success;
  666.         $this->output();
  667.     }
  668.  
  669.  
  670.     /**
  671.      * @description Adding/editing issue type fields
  672.      *
  673.      * @var $issue_type_fields - array of issue type fields
  674.      *      with data (issue_field_id, name, issue_type_id, show_in_popup)
  675.      */
  676.     public function saveIssueTypeFieldAction()
  677.     {
  678.         $success = false;
  679.         $results = [];
  680.         $issue_type_fields = $this->_input['issue_type_fields'];
  681.         /*
  682.         // test
  683.         $issue_type_fields = [
  684.             ['issue_field_id' => 2, 'name' => 'qwe10', 'issue_type_id' => 1, 'show_in_popup' => 0],
  685.             ['issue_field_id' => 5, 'name' => 'qwe11', 'issue_type_id' => 1, 'show_in_popup' => 0],
  686.             ['name' => 'qwe12', 'issue_type_id' => 1, 'show_in_popup' => 0]
  687.         ];
  688.         */
  689.         if (is_array($issue_type_fields) && count($issue_type_fields) > 0) {
  690.             foreach ($issue_type_fields as $issue_type_field) {
  691.                 /*
  692.                  *  Add or update issue type field
  693.                  */
  694.                 $result = IssueLog::saveIssueTypeField(
  695.                     intval($issue_type_field['field_id']),
  696.                     mysql_real_escape_string($issue_type_field['field_name']),
  697.                     intval($issue_type_field['issue_type_id']),
  698.                     ($issue_type_field['show_in_popup'] > 0) ? 1 : 0,
  699.                     ($issue_type_field['inactive'] > 0) ? 1 : 0
  700.                 );
  701.                 if ($result) {
  702.                     $success = true;
  703.                     $results[] = $result;
  704.                 } else {
  705.                     $success = false;
  706.                 }
  707.             }
  708.         }
  709.         $this->_result['issue_type_fields'] = $results;
  710.         $this->_result['success'] = $success;
  711.         $this->output();
  712.     }
  713.  
  714.     /**
  715.      * @description Getting issue type fieldlist
  716.      * @var $issue_type_id
  717.      */
  718.     public function issueTypeFieldlistAction()
  719.     {
  720.         $issue_type_id = intval($this->_input['issue_type_id']);
  721.         $show_in_popup = (isset($this->_input['show_in_popup'])) ? intval($this->_input['show_in_popup']) : -1;
  722.         $inactive = (isset($this->_input['inactive'])) ? intval($this->_input['inactive']) : -1;
  723.  
  724.         if ($issue_type_id > 0) {
  725.             $fieldlist = IssueLog::getIssueTypeFieldList([$issue_type_id], $show_in_popup, $inactive);
  726.         } else {
  727.             $fieldlist = IssueLog::getIssueTypeFieldList([], $show_in_popup, $inactive);
  728.         }
  729.         $this->_result['fieldlist'] = $fieldlist;
  730.         $this->_result['success'] = ($fieldlist) ? true : false;
  731.         $this->output();
  732.     }
  733.  
  734.     /**
  735.      * @description Save checklist ordering
  736.      *
  737.      * @var $issue_id
  738.      * @var $ordering - array of checkpoint ids and orderings
  739.      */
  740.     public function saveChecklistOrderingAction()
  741.     {
  742.         $success = false;
  743.         $ordering = $this->_input['ordering'];
  744.         $issue_id = intval($this->_input['issue_id']);
  745.  
  746.         if (is_array($ordering) && count($ordering) > 0) {
  747.             foreach ($ordering as $item) {
  748.                 if (isset($item['checkpoint_id']) && isset($item['ordering'])) {
  749.                     $q = "UPDATE issue_checkpoint
  750.                            SET ordering = " . intval($item['ordering']) . "
  751.                          WHERE issuelog_id = " . $issue_id . "
  752.                          AND id = " . $item['checkpoint_id'];
  753.                     $this->_db->query($q);
  754.                 }
  755.             }
  756.             $this->_result['ordering'] = $ordering;
  757.             $success = true;
  758.         }
  759.  
  760.         $checklist = array();
  761.         if ($issue_id > 0) {
  762.             $q = "SELECT * FROM issue_checkpoint WHERE issuelog_id = " . $issue_id . " ORDER BY ordering, id ASC";
  763.             $checklist = $this->_dbr->getAll($q);
  764.         }
  765.         $this->_result['checklist'] = $checklist;
  766.         $this->_result['success'] = $success;
  767.         $this->output();
  768.     }
  769.  
  770.     /**
  771.      * @description Set days for ping after no activity
  772.      * @var $days - after X days send email
  773.      *      via cron if no activity in issue
  774.      * @var $issue_id
  775.      */
  776.     public function setPingAfterNoActivityAction()
  777.     {
  778.         $success = false;
  779.         $days = $this->_input['days'];
  780.         $issue_id = $this->_input['issue_id'];
  781.         $user_id = $user_id = $this->_loggedUser->get('id');
  782.         if ($days >= 0 && $issue_id > 0 && $user_id > 0) {
  783.             IssueLog::savePingDays($issue_id, $user_id, $days);
  784.             $this->_result['ping_days'] = $days;
  785.             $success = true;
  786.         }
  787.         $this->_result['success'] = $success;
  788.         $this->output();
  789.     }
  790.  
  791.     /**
  792.      * @description Getting additional description fields
  793.      * @var $issue_id
  794.      */
  795.     public function additionalDescriptionFieldsAction()
  796.     {
  797.         $success = false;
  798.         $issue_id = $this->_input['issue_id'];
  799.         if ($issue_id > 0) {
  800.             $issue = new IssueLog($issue_id);
  801.             $this->_result['fields'] = $issue->getAdditionalFields();
  802.             $success = true;
  803.         }
  804.         $this->_result['success'] = $success;
  805.         $this->output();
  806.     }
  807.  
  808.     /**
  809.      * @description Getting additional description fields
  810.      * @var $issue_id
  811.      */
  812.     public function saveAdditionalFieldsAction()
  813.     {
  814.         $success = false;
  815.         $issue_id = $this->_input['issue_id'];
  816.         $fields = $this->_input['additional_fields'];
  817.  
  818.          //test
  819.         /*
  820.         $fields['additional_fields'] = array(
  821.             0 => array('field_id' => 27, 'value' => 'new field test value 1'),
  822.             1 => array('field_id' => 25, 'value' => 'new field test value 2'),
  823.             2 => array('field_id' => 26, 'value' => 'new field test value 3')
  824.         );
  825.         print_r($fields); die();
  826.         */
  827.  
  828.         if ($issue_id > 0 && count($fields) > 0) {
  829.             $issue = new IssueLog($issue_id);
  830.             foreach ($fields as $field) {
  831.                 $issue->saveAdditionalField( intval($field['field_id']), $field['value']);
  832.             }
  833.             $success = true;
  834.             $this->_result['fields'] = $issue->getAdditionalFields();
  835.         }
  836.         $this->_result['success'] = $success;
  837.         $this->output();
  838.     }
  839.  
  840.     /**
  841.      * @description Getting all checklist titles
  842.      */
  843.     public function getChecklistTitlesAction()
  844.     {
  845.         $q = "SELECT
  846.                  DISTINCT issue_checkpoint.issuelog_id as issue_id,
  847.                  COUNT(issue_checkpoint.id) as `checkpoints`,
  848.                  issuelog.checklist_title
  849.              FROM issue_checkpoint
  850.                  LEFT JOIN issuelog ON issuelog.id = issue_checkpoint.issuelog_id
  851.              WHERE 1
  852.              GROUP BY issue_checkpoint.issuelog_id";
  853.         $this->_result['titles'] = $this->_dbr->getAll($q);
  854.         $this->_result['success'] = true;
  855.         $this->output();
  856.     }
  857.  
  858.     /**
  859.      * @description Getting all checklist titles
  860.      */
  861.     public function importChecklistAction()
  862.     {
  863.         $success = false;
  864.         $issue_id_from = $this->_input['issue_id_from'];
  865.         $issue_id_to = $this->_input['issue_id_to'];
  866.         if ($issue_id_from > 0 && $issue_id_to > 0 && $issue_id_to != $issue_id_from) {
  867.             IssueLog::importChecklist($issue_id_from, $issue_id_to);
  868.             $success = true;
  869.         }
  870.         $checklist = IssueLog::getChecklist($issue_id_to);
  871.         $this->_result['checklist'] = IssueLog::checklistReorder($issue_id_to, $checklist);
  872.         $this->_result['success'] = $success;
  873.         $this->output();
  874.     }
  875.  
  876.  
  877.     /**
  878.      * @description Saving checklist title
  879.      * @var $title - checklist title
  880.      * @var $issue_id
  881.      */
  882.     public function saveChecklistTitleAction()
  883.     {
  884.         $success = false;
  885.         $issue_id = intval($this->_input['issue_id']);
  886.         $title = $this->_input['checklist_title'];
  887.         if ($issue_id > 0)
  888.         {
  889.             $issue = new IssueLog($issue_id);
  890.             $issue->set('checklist_title', $title);
  891.             $issue->update();
  892.             $success = true;
  893.         }
  894.         $this->_result['title'] = $title;
  895.         $this->_result['success'] = $success;
  896.         $this->output();
  897.     }
  898.  
  899.  
  900.     /**
  901.      * @description Duplicating the issue
  902.      */
  903.     public function duplicateIssueAction()
  904.     {
  905.         $success = false;
  906.         $issue_id = intval($this->_input['issue_id']);
  907.  
  908.         if ($issue_id > 0)
  909.         {
  910.             $new_issue_id = IssueLog::duplicateIssue($issue_id, [
  911.                 // What will we duplicate?
  912.                 'tags' => (isset($this->_input['tags'])) ? intval($this->_input['tags']) : 1,
  913.                 'details' => (isset($this->_input['details'])) ? intval($this->_input['details']) : 1,
  914.                 'description' => (isset($this->_input['description'])) ? intval($this->_input['description']) : 1,
  915.                 'comments' => (isset($this->_input['comments'])) ? intval($this->_input['comments']) : 1,
  916.                 'checklist' => (isset($this->_input['checklist'])) ? intval($this->_input['checklist']) : 1,
  917.                 'images' => (isset($this->_input['images'])) ? intval($this->_input['images']) : 1,
  918.                 'files' => (isset($this->_input['files'])) ? intval($this->_input['files']) : 1,
  919.                 'videos' => (isset($this->_input['videos'])) ? intval($this->_input['videos']) : 1
  920.             ]);
  921.             $this->_result['new_issue_id'] = $new_issue_id;
  922.  
  923.             $res = IssueLog::findByFilterParams(array('page_id' => $new_issue_id));
  924.             $this->_result['issue'] = $res['issue_list'][0];
  925.             $success = true;
  926.         }
  927.  
  928.         $this->_result['success'] = $success;
  929.         $this->output();
  930.     }
  931.  
  932.  
  933.     /**
  934.      * @description Set close_notif option
  935.      */
  936.     public function closeNotifAction()
  937.     {
  938.         $success = false;
  939.         $issue_id = intval($this->_input['issue_id']);
  940.         $username = $this->_loggedUser->get('username');
  941.  
  942.         if ($issue_id > 0 && $username)
  943.         {
  944.             $data = IssueLog::closeNotifToogle($issue_id, $username);
  945.             $this->_result['close_notif'] = $data;
  946.             $success = true;
  947.         }
  948.  
  949.         $this->_result['success'] = $success;
  950.         $this->output();
  951.     }
  952. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement