382 lines
11 KiB
PHP
382 lines
11 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
class Fields
|
||
|
|
{
|
||
|
|
|
||
|
|
private $db = null;
|
||
|
|
|
||
|
|
public function __construct($db)
|
||
|
|
{
|
||
|
|
$this->db = $db;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get_client_fields($agency_id, $client_id = 0, $user_id = 0)
|
||
|
|
{
|
||
|
|
|
||
|
|
if($user_id == 0){
|
||
|
|
$user_id = (int)$_SESSION['id'];
|
||
|
|
}
|
||
|
|
$result = array();
|
||
|
|
if ($agency_id > 0) {
|
||
|
|
$sql = "SELECT * FROM client_req_fields WHERE agency_id = $agency_id AND is_client > 0";
|
||
|
|
$q = $this->db->query($sql);
|
||
|
|
while ($r = $this->db->fetch_assoc($q)) {
|
||
|
|
|
||
|
|
$is_see = true;
|
||
|
|
if($r['is_no_see'] == 1 && !empty($r['users_no_see'])){
|
||
|
|
$is_see = false;
|
||
|
|
$arrNoSeeUsers = array();
|
||
|
|
$tempArrNoSee = json_decode($r['users_no_see'], true);
|
||
|
|
foreach($tempArrNoSee as $u){
|
||
|
|
if(strpos($u, 'all_') !== false){
|
||
|
|
$manId = str_replace('all_', '', $u);
|
||
|
|
$sql_u = "SELECT id FROM `users`
|
||
|
|
WHERE id = $manId OR
|
||
|
|
id_manager = $manId OR
|
||
|
|
id_manager IN (SELECT id FROM users WHERE id_manager = $manId)";
|
||
|
|
$q_u = $this->db->query($sql_u);
|
||
|
|
while($r_u = $this->db->fetch_row($q_u)){
|
||
|
|
$arrNoSeeUsers[] = (int)$r_u[0];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$arrNoSeeUsers[] = (int)$u;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(in_array($user_id, $arrNoSeeUsers)){
|
||
|
|
$is_see = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if(!$is_see) continue;
|
||
|
|
|
||
|
|
if (!empty($r['params'])) {
|
||
|
|
$r['params'] = json_decode($r['params'], true);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($r['is_podgroup'] > 0) {
|
||
|
|
$r['podgroups'] = array();
|
||
|
|
$r['podgroups_select'] = array();
|
||
|
|
|
||
|
|
$sql_p = "SELECT * FROM client_req_fields_podgroup WHERE group_id = $r[id] AND agency_id = $agency_id";
|
||
|
|
$q_p = $this->db->query($sql_p);
|
||
|
|
while ($r_p = $this->db->fetch_assoc($q_p)) {
|
||
|
|
if (!empty($r_p['params'])) {
|
||
|
|
$r_p['params'] = json_decode($r_p['params'], true);
|
||
|
|
}
|
||
|
|
$r['podgroups_select'][] = array('id' => $r_p['id'], 'title' => $r_p['title']);
|
||
|
|
$r['podgroups'][] = $r_p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['fields'][] = $r;
|
||
|
|
}
|
||
|
|
|
||
|
|
//Значения
|
||
|
|
$values = array();
|
||
|
|
if ($client_id > 0) {
|
||
|
|
$sql_val = "SELECT * FROM client_data WHERE client_id=$client_id";
|
||
|
|
$q_val = $this->db->query($sql_val);
|
||
|
|
while ($r_val = $this->db->fetch_assoc($q_val)) {
|
||
|
|
if ($r_val['type'] == 1) {
|
||
|
|
$values['model_' . $r_val['field_id']] = $r_val['value'];
|
||
|
|
} else if ($r_val['type'] == 2) {
|
||
|
|
$values['modelp_' . $r_val['field_id']] = $r_val['value'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['values'] = $values;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get_req_fields($agency_id, $req_id = 0, $user_id = 0)
|
||
|
|
{
|
||
|
|
if($user_id == 0){
|
||
|
|
$user_id = (int)$_SESSION['id'];
|
||
|
|
}
|
||
|
|
$result = array();
|
||
|
|
if ($agency_id > 0) {
|
||
|
|
$sql = "SELECT * FROM client_req_fields WHERE agency_id = $agency_id AND is_req > 0";
|
||
|
|
$q = $this->db->query($sql);
|
||
|
|
while ($r = $this->db->fetch_assoc($q)) {
|
||
|
|
|
||
|
|
$is_see = true;
|
||
|
|
if($r['is_no_see'] == 1 && !empty($r['users_no_see'])){
|
||
|
|
$is_see = false;
|
||
|
|
$arrNoSeeUsers = array();
|
||
|
|
$tempArrNoSee = json_decode($r['users_no_see'], true);
|
||
|
|
foreach($tempArrNoSee as $u){
|
||
|
|
if(strpos($u, 'all_') !== false){
|
||
|
|
$manId = str_replace('all_', '', $u);
|
||
|
|
$sql_u = "SELECT id FROM `users`
|
||
|
|
WHERE id = $manId OR
|
||
|
|
id_manager = $manId OR
|
||
|
|
id_manager IN (SELECT id FROM users WHERE id_manager = $manId)";
|
||
|
|
$q_u = $this->db->query($sql_u);
|
||
|
|
while($r_u = $this->db->fetch_row($q_u)){
|
||
|
|
$arrNoSeeUsers[] = (int)$r_u[0];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$arrNoSeeUsers[] = (int)$u;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(in_array($user_id, $arrNoSeeUsers)){
|
||
|
|
$is_see = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if(!$is_see) continue;
|
||
|
|
|
||
|
|
if (!empty($r['params'])) {
|
||
|
|
$r['params'] = json_decode($r['params'], true);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($r['is_podgroup'] > 0) {
|
||
|
|
$r['podgroups'] = array();
|
||
|
|
$r['podgroups_select'] = array();
|
||
|
|
|
||
|
|
$sql_p = "SELECT * FROM client_req_fields_podgroup WHERE group_id = $r[id] AND agency_id = $agency_id";
|
||
|
|
$q_p = $this->db->query($sql_p);
|
||
|
|
while ($r_p = $this->db->fetch_assoc($q_p)) {
|
||
|
|
if (!empty($r_p['params'])) {
|
||
|
|
$r_p['params'] = json_decode($r_p['params'], true);
|
||
|
|
}
|
||
|
|
$r['podgroups_select'][] = array('id' => $r_p['id'], 'title' => $r_p['title']);
|
||
|
|
$r['podgroups'][] = $r_p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['fields'][] = $r;
|
||
|
|
}
|
||
|
|
|
||
|
|
//Значения
|
||
|
|
$values = array();
|
||
|
|
if ($req_id > 0) {
|
||
|
|
$sql_val = "SELECT * FROM requisition_data WHERE req_id=$req_id";
|
||
|
|
$q_val = $this->db->query($sql_val);
|
||
|
|
while ($r_val = $this->db->fetch_assoc($q_val)) {
|
||
|
|
if ($r_val['type'] == 1) {
|
||
|
|
$values['model_' . $r_val['field_id']] = $r_val['value'];
|
||
|
|
} else if ($r_val['type'] == 2) {
|
||
|
|
$values['modelp_' . $r_val['field_id']] = $r_val['value'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['values'] = $values;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_edit($field_models_params, $client_id)
|
||
|
|
{
|
||
|
|
|
||
|
|
$user_id = (int)$_SESSION['id'];
|
||
|
|
if($user_id == 0) die();
|
||
|
|
|
||
|
|
$agency = new User;
|
||
|
|
$agency->get($_SESSION['id']);
|
||
|
|
$agency_id = $agency->agencyId;
|
||
|
|
|
||
|
|
//Поля которые может редактировать сотрудник
|
||
|
|
$fields_can_edid = array();
|
||
|
|
$sql_field = "SELECT * FROM `client_req_fields` WHERE agency_id = {$agency_id}";
|
||
|
|
$q_fields = $this->db->query($sql_field);
|
||
|
|
while($r_fields = $this->db->fetch_assoc($q_fields)){
|
||
|
|
$tempArrNoSee = json_decode($r_fields['users_no_see'], true);
|
||
|
|
if($r_fields['is_no_see'] == 1 && !empty($r_fields['users_no_see']) && !empty($tempArrNoSee)){
|
||
|
|
|
||
|
|
$arrNoSeeUsers = array();
|
||
|
|
|
||
|
|
foreach($tempArrNoSee as $u){
|
||
|
|
if(strpos($u, 'all_') !== false){
|
||
|
|
$manId = str_replace('all_', '', $u);
|
||
|
|
$sql_u = "SELECT id FROM `users`
|
||
|
|
WHERE id = $manId OR
|
||
|
|
id_manager = $manId OR
|
||
|
|
id_manager IN (SELECT id FROM users WHERE id_manager = $manId)";
|
||
|
|
$q_u = $this->db->query($sql_u);
|
||
|
|
while($r_u = $this->db->fetch_row($q_u)){
|
||
|
|
$arrNoSeeUsers[] = (int)$r_u[0];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$arrNoSeeUsers[] = (int)$u;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(in_array($_SESSION['id'], $arrNoSeeUsers)){
|
||
|
|
$fields_can_edid[] = (int)$r_fields['id'];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$fields_can_edid[] = (int)$r_fields['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(!empty($fields_can_edid)){
|
||
|
|
$sql_del = "DELETE FROM client_data WHERE client_id=$client_id and field_id in (".implode(',', $fields_can_edid).")";
|
||
|
|
$this->db->query($sql_del);
|
||
|
|
|
||
|
|
if (!empty($field_models_params)) {
|
||
|
|
foreach ($field_models_params as $key => $field_models) {
|
||
|
|
$arrField = explode("_", $key);
|
||
|
|
$typeField = 1;
|
||
|
|
if ($arrField[0] == 'modelp') {
|
||
|
|
$typeField = 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
$value = json_encode($field_models, JSON_UNESCAPED_UNICODE);
|
||
|
|
$field_id = (int)$arrField[1];
|
||
|
|
$sql_in = "INSERT INTO client_data SET " .
|
||
|
|
" client_id=$client_id, " .
|
||
|
|
" field_id=$field_id, " .
|
||
|
|
" type=$typeField, " .
|
||
|
|
" value='$value'";
|
||
|
|
|
||
|
|
$this->db->query($sql_in);
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_edit_data($section, $source_id, $fields, $user_id = 0) {
|
||
|
|
|
||
|
|
|
||
|
|
if($user_id == 0) {
|
||
|
|
$user_id = (int)$_SESSION['id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$agency = new User;
|
||
|
|
$agency->get($user_id);
|
||
|
|
$agency_id = $agency->agencyId;
|
||
|
|
|
||
|
|
//Поля которые может редактировать сотрудник
|
||
|
|
$fields_can_edid = array();
|
||
|
|
$sql_field = "SELECT * FROM `client_req_fields` WHERE agency_id = {$agency_id}";
|
||
|
|
|
||
|
|
$q_fields = mysql_query($sql_field);
|
||
|
|
|
||
|
|
while($r_fields = mysql_fetch_assoc($q_fields)){
|
||
|
|
|
||
|
|
$tempArrNoSee = json_decode($r_fields['users_no_see'], true);
|
||
|
|
|
||
|
|
if($r_fields['is_no_see'] == 1 && !empty($r_fields['users_no_see']) && !empty($tempArrNoSee)){
|
||
|
|
|
||
|
|
$arrNoSeeUsers = array();
|
||
|
|
|
||
|
|
foreach($tempArrNoSee as $u){
|
||
|
|
|
||
|
|
if(strpos($u, 'all_') !== false){
|
||
|
|
$manId = str_replace('all_', '', $u);
|
||
|
|
$sql_u = "SELECT id FROM `users`
|
||
|
|
WHERE id = $manId OR
|
||
|
|
id_manager = $manId OR
|
||
|
|
id_manager IN (SELECT id FROM users WHERE id_manager = $manId)";
|
||
|
|
|
||
|
|
$q_u = mysql_query($sql_u);
|
||
|
|
while($r_u = mysql_fetch_row($q_u)){
|
||
|
|
$arrNoSeeUsers[] = (int)$r_u[0];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$arrNoSeeUsers[] = (int)$u;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(in_array($user_id, $arrNoSeeUsers)){
|
||
|
|
$fields_can_edid[] = (int)$r_fields['id'];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$fields_can_edid[] = (int)$r_fields['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$errors = [];
|
||
|
|
$sqls = [];
|
||
|
|
if(!empty($fields_can_edid)){
|
||
|
|
|
||
|
|
|
||
|
|
if ($section == 'clients'){
|
||
|
|
$sql_del = "DELETE FROM client_data WHERE client_id=$source_id and field_id in (".implode(',', $fields_can_edid).")";
|
||
|
|
|
||
|
|
} else if ($section == 'requisitions')
|
||
|
|
$sql_del = "DELETE FROM requisition_data WHERE req_id=$source_id and field_id in (".implode(',', $fields_can_edid).")";
|
||
|
|
|
||
|
|
if (!mysql_query($sql_del))
|
||
|
|
$errors[] = mysql_errorInfo();
|
||
|
|
|
||
|
|
|
||
|
|
foreach ($fields as $id => $data) {
|
||
|
|
|
||
|
|
$type = null;
|
||
|
|
if (isset($data['type']))
|
||
|
|
$type = $data['type'];
|
||
|
|
|
||
|
|
$value = null;
|
||
|
|
if (isset($data['value']))
|
||
|
|
$value = $data['value'];
|
||
|
|
|
||
|
|
if (!$id || is_null($value))
|
||
|
|
continue;
|
||
|
|
|
||
|
|
if (is_array($value))
|
||
|
|
$value = array_values($value);
|
||
|
|
|
||
|
|
if ($section == 'clients') {
|
||
|
|
if (!is_null($type)) {
|
||
|
|
$sql = "INSERT INTO `client_data`
|
||
|
|
SET `client_id`='$source_id',
|
||
|
|
`field_id`='$id',
|
||
|
|
`type`='$type',
|
||
|
|
`value`='" . json_encode($value, JSON_UNESCAPED_UNICODE) . "'";
|
||
|
|
} else {
|
||
|
|
$sql = "INSERT INTO `client_data`
|
||
|
|
SET `client_id`='$source_id',
|
||
|
|
`field_id`='$id',
|
||
|
|
`type`=IF((SELECT `is_podgroup` FROM `client_req_fields` WHERE `id`='$id' AND `is_client` > 0) > 0, 2, 1),
|
||
|
|
`value`='" . json_encode($value, JSON_UNESCAPED_UNICODE) . "'";
|
||
|
|
}
|
||
|
|
} else if ($section == 'requisitions') {
|
||
|
|
if (!is_null($type)) {
|
||
|
|
$sql = "INSERT INTO `requisition_data`
|
||
|
|
SET `req_id`='$source_id',
|
||
|
|
`field_id`='$id',
|
||
|
|
`type`='$type',
|
||
|
|
`value`='" . json_encode($value, JSON_UNESCAPED_UNICODE) . "'";
|
||
|
|
} else {
|
||
|
|
$sql = "INSERT INTO `requisition_data`
|
||
|
|
SET `req_id`='$source_id',
|
||
|
|
`field_id`='$id',
|
||
|
|
`type`=IF((SELECT `is_podgroup` FROM `client_req_fields` WHERE `id`='$id' AND `is_req` > 0) > 0, 2, 1),
|
||
|
|
`value`='" . json_encode($value, JSON_UNESCAPED_UNICODE) . "'";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$sqls[] = $sql;
|
||
|
|
if (!mysql_query($sql))
|
||
|
|
$errors[] = mysql_errorInfo();
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (count($errors))
|
||
|
|
return [
|
||
|
|
'success' => false,
|
||
|
|
'errors' => $errors,
|
||
|
|
];
|
||
|
|
else
|
||
|
|
return [
|
||
|
|
'success' => true,
|
||
|
|
'sql' => $sqls,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
?>
|