| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021 |
- <?php
- /**
- * $_SESSION['TableAcl_cache'][$tableID] = array(
- * [db] => DB zasob ID
- * [name] => Table name
- * [opis] => Table opis
- * [fields] => array(
- * [$fieldID] => array(
- * [name] => name
- * [perms] => perms (FORM_TREAT)
- * [opis] => opis
- * )
- * )
- * [types] => array(
- * [$fieldID] => array(
- * [type] => type
- * [null] => bool
- * [default] => default value
- * )
- * )
- * );
- */
- class TableAcl {
- private $_zasobID = '';
- private $_db = '';
- private $_name = '';
- private $_label = '';
- private $_opis = '';
- private $_fields = array();
- private $_types = array();
- private $_virtualFieldsIdList = array();
- public function __construct($zasobID) {
- $this->_zasobID = $zasobID;
- }
- public function getID() {
- return $this->_zasobID;
- }
- public function setName($name) {
- $this->_name = $name;
- }
-
- public function setNameByTableId($tableID) {
- //used for init without knowing table name
- $sql="select `DESC` from CRM_LISTA_ZASOBOW where ID=".$tableID." and `TYPE`='TABELA'";
- $res=DB::query($sql);
- $res_=DB::fetch($res);
- //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
- self::setName($res->DESC);
-
- }
- public function getName() {
- return $this->_name;
- }
- public function setOpis($opis) {
- $this->_opis = $opis;
- }
- public function getOpis() {
- return $this->_opis;
- }
- public function setLabel($label) {
- $this->_label = $label;
- }
- public function getLabel() {
- return $this->_label;
- }
- public function getRawLabel($posLimit = 20) {
- $label = $this->_label;
- if (empty($label) && !empty($this->_opis)) {
- $label = $this->_opis;
- if (mb_strlen($this->_opis) > $posLimit) {
- $pos = strpos($this->_opis, ' - ');
- if ($pos > $posLimit || $pos < 5) {
- $pos = $posLimit;
- $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
- } else {
- $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
- }
- }
- }
- if (empty($label)) {
- $label = $this->_name;
- }
- return $label;
- }
- public function getShortLabel($posLimit = 20) {
- $shortLabel = $this->getRawLabel($posLimit);
- $opis = $this->_opis;
- $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
- return $shortLabel;
- }
- public function getLongLabel($posLimit = 30) {
- $longLabel = $this->getRawLabel($posLimit);
- $opis = $this->_opis;
- if ($longLabel != $this->_name) {
- $longLabel .= ' <em>' . $this->_name . '</em>';
- }
- $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
- return $longLabel;
- }
- public function setDB($db) {
- $this->_db = $db;
- }
- public function getDB() {
- return $this->_db;
- }
- public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
- $field = array();
- $field['name'] = $name;
- $field['perms'] = '';
- $field['opis'] = $opis;
- $field['sort_prio'] = $sort_prio;
- $field['label'] = $label;
- $this->_fields[$fieldID] = $field;
- }
- public function getTableDbId($tableID) {
- return $this->_db;
- }
- public function getField($fieldID) {
- return $this->_fields[$fieldID];
- }
- public function hasField($fieldID) {
- return array_key_exists($fieldID, $this->_fields);
- }
- public function removeField($fieldID) {
- if (array_key_exists($fieldID, $this->_fields)) {
- unset($this->_fields[$fieldID]);
- }
- }
- public function getFields() {
- return $this->_fields;
- }
- public function setFieldPerms($fieldID, $perms) {
- if (array_key_exists($fieldID, $this->_fields)) {
- $this->_fields[$fieldID]['perms'] .= $perms;
- }
- }
- public function getFieldPerms($fieldID) {
- if (array_key_exists($fieldID, $this->_fields)) {
- $perms = V::get('perms', '', $this->_fields[$fieldID]);
- if ($perms) {
- return implode(',', array_unique(str_split($perms)));
- }
- }
- return '';
- }
- public function hasFieldPerm($fieldID, $perm) {
- if (array_key_exists($fieldID, $this->_fields)) {
- if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
- return true;
- }
- return false;
- }
- return false;
- }
- public function getFieldIdByName($fieldName) {
- $fieldID = 0;
- if (empty($fieldName)) {
- return;
- }
- foreach ($this->_fields as $kID => $vField) {
- if ($vField['name'] == $fieldName) {
- $fieldID = $kID;
- }
- }
- return $fieldID;
- }
- public function hasSuperAccessPerms() {
- foreach ($this->_fields as $kFldID => $vFld) {
- if ($this->hasFieldPerm($kFldID, 'S')) {
- return true;
- }
- else if ($this->hasFieldPerm($kFldID, 'V')) {
- return true;
- }
- }
- return false;
- }
- public function hasPermSuperWrite() {
- foreach ($this->_fields as $kFldID => $vFld) {
- if ($this->hasFieldPerm($kFldID, 'S')) {
- return true;
- }
- }
- return false;
- }
- /**
- *
- */
- public function canWriteRecord($record) {
- $dbgArr = array();
- $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
- $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
- $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
- $dbgArr['user_groups'] = User::getLdapGroupsNames();
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">dbgArr (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dbgArr);echo'</pre>';}
- if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
- return true;
- }
- if ($dbgArr['record_write']) {
- if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group write</p>';}
- return true;
- }
- } else {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group write not set</p>';}
- return true;
- }
- return false;
- }
- public function canReadRecord($record) {
- $dbgArr = array();
- $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
- $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
- $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
- $dbgArr['user_groups'] = User::getLdapGroupsNames();
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">record('.$record->ID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dbgArr);echo'</pre>';}
- if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
- return true;
- }
- if ($dbgArr['record_read']) {
- if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group read</p>';}
- return true;
- }
- } else {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group read not set</p>';}
- return true;
- }
- return false;
- }
- /**
- * @param $taskPerm - 'C', 'W', 'R'
- */
- public function isAllowed($fieldID, $taskPerm, $record = null) {
- if (!in_array($taskPerm, array('C', 'W', 'R'))) {
- return false;
- }
- $adminFields = array();
- $adminFields[] = 'ID';
- $adminFields[] = 'A_RECORD_CREATE_DATE';
- $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
- $adminFields[] = 'A_RECORD_UPDATE_DATE';
- $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
- $fieldName = $this->_fields[$fieldID]['name'];
- if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
- return true;
- }
- // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
- // check 'O' - can read field even if cant read field but can read record
- if(V::get('DBG_ACL', '', $_REQUEST) > 1){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('Field'=>$fieldID.'('.$fieldName.')'
- ,'taskPerm'=>$taskPerm
- ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
- ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
- ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
- ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
- ));echo'</pre>'; }
- if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
- if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
- return true;
- } else if ($taskPerm == 'R'
- && $record
- && $this->hasFieldPerm($fieldID, 'O')
- && ($this->canReadRecord($record) || $this->canWriteRecord($record))
- ) {
- return true;// 'WO' or 'CO'
- }
- return false;
- }
- // check 'R' - require can read record, or V - Super View
- if ($taskPerm == 'R') {
- if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
- return true;
- } else {
- return false;
- }
- }
- // 'C' and 'W' require colType
- $colType = $this->getFieldTypeById($fieldID);
- if (!$colType) {
- return false;
- }
- if ($taskPerm == 'W') {
- if ($record) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: '.$fieldID.', canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($fieldID, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($fieldID, 'W') . '))';}
- return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
- }
- }
- return true;
- }
- /**
- * @param $taskPerm - 'C', 'W'
- */
- public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
- $out = '';
- if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
- if ($taskPerm == 'R') {
- $out .= 'Brak uprawnień do odczytu';
- }
- else if ($taskPerm == 'W') {
- $out .= 'Brak uprawnień do zapisu';
- } else {
- $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
- }
- return $out;
- }
- $colName = $this->_fields[$fieldID]['name'];
- if ($colName == 'ID') {
- return $out;
- }
- $colType = $this->getFieldTypeById($fieldID);
- if (!$colType) {
- $out .= 'Error - unknown type';
- return $out;
- }
- Lib::loadClass('Typespecial');
- $typeSpecial = Typespecial::getInstance($fieldID, $colName);
- $html = new stdClass();
- $html->_params = array();
- $html->tag = 'input';
- $html->cnt = '';
- $html->attrs = array();
- $html->attrs['id'] = $fName;
- $html->attrs['name'] = $fName;
- $html->attrs['type'] = 'text';
- $html->attrs['value'] = htmlspecialchars($fValue);
- if (isset($params['tabindex'])) {
- $html->attrs['tabindex'] = $params['tabindex'];
- }
- if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
- $html->attrs['disabled'] = 'disabled';
- }
- $maxGrid = V::get('maxGrid', 10, $params);
- if (substr($colType['type'], 0, 3) == 'int'
- || substr($colType['type'], 0, 7) == 'tinyint'
- || substr($colType['type'], 0, 8) == 'smallint'
- || substr($colType['type'], 0, 6) == 'bigint'
- ) {
- //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
- $html->attrs['type'] = 'number';
- $html->attrs['class'][] = 'input-small';
- }
- else if (substr($colType['type'], 0, 6) == 'double') {
- $html->attrs['type'] = 'text';
- $html->attrs['class'][] = 'input-small';
- }
- else if (substr($colType['type'], 0, 7) == 'decimal') {
- $html->attrs['type'] = 'text';
- $html->attrs['class'][] = 'input-small';
- }
- else if (substr($colType['type'], 0, 7) == 'varchar'
- || substr($colType['type'], 0, 4) == 'char'
- ) {
- //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
- $html->attrs['type'] = 'text';
- $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
- if ($maxLength > 0) {
- $html->attrs['maxlength'] = $maxLength;
- }
- $valLength = strlen($fValue);
- if (isset($params['widthClass'])) {
- if ($params['widthClass'] == 'inside-modal') {
- $html->attrs['style'] = 'width:98%;';
- } else {
- $html->attrs['style'] = 'width:98%;';
- }
- } else {
- /*
- if ($maxLength < 11) {
- $html->attrs['class'][] = 'span2';
- } else if ($maxLength < 31) {
- $html->attrs['class'][] = 'span5';
- } else if ($maxLength < 51) {
- $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
- } else if ($maxLength < 101) {
- $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
- } else {
- $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
- }
- */
- }
- }
- else if (substr($colType['type'], 0, 4) == 'date') {
- $testDatePicker = true;
- if ($testDatePicker) {
- $html->attrs['type'] = 'text';
- $html->_params[] = 'date';
- if (substr($colType['type'], 0, 8) == 'datetime') {
- $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
- $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
- $html->attrs['maxlength'] = 19;
- } else {
- $html->attrs['class'][] = 'se_type-date';// datetimepicker';
- $html->attrs['maxlength'] = 10;
- }
- if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
- $html->attrs['value'] = '';
- }
- } else {
- $html->attrs['type'] = 'date';
- }
- }
- else if ($colType['type'] == 'time') {
- $testDatePicker = true;
- if ($testDatePicker) {
- $html->attrs['type'] = 'text';
- $html->_params[] = 'time';
- $html->attrs['class'][] = 'se_type-time';// datetimepicker';
- $html->attrs['data-format'] = 'hh:mm:ss';
- $html->attrs['maxlength'] = 8;
- if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
- $html->attrs['value'] = '';
- }
- } else {
- $html->attrs['type'] = 'time';
- }
- }
- else if (substr($colType['type'], 0, 4) == 'enum') {
- unset($html->attrs['type']);
- unset($html->attrs['value']);
- $html->tag = 'select';
- $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
- $selValue = $fValue;
- if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
- if ($taskPerm == 'C') {
- $selValue = $colType['default'];
- } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
- $selValue = $colType['default'];
- }
- }
- $html->cnt .= '<option value="">' . "" . '</option>';
- if (!empty($selValue) && !in_array($selValue, $values)) {
- $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
- }
- foreach ($values as $val) {
- $sel = ($selValue == $val)? ' selected="selected"' : '';
- $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
- }
- }
- else if (substr($colType['type'], 0, 4) == 'text'
- || substr($colType['type'], 0, 8) == 'tinytext'
- || substr($colType['type'], 0, 10) == 'mediumtext'
- || substr($colType['type'], 0, 8) == 'longtext'
- ) {
- $html->tag = 'textarea';
- $html->cnt = htmlspecialchars($fValue);
- if (isset($params['widthClass'])) {
- if ($params['widthClass'] == 'inside-modal') {
- $html->attrs['style'] = 'width:98%;';
- } else {
- $html->attrs['style'] = 'width:98%;';
- }
- } else {
- //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
- }
- $html->attrs['rows'] = '3';
- unset($html->attrs['type']);
- unset($html->attrs['value']);
- }
- else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
- else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
- else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
- else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
- else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
- else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
- else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
- else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
- else {
- return 'unknown Type "'.$colType['type'].'"';
- }
- $html->attrs['class'][] = 'form-control';
- $attrsOut = array();
- foreach ($html->attrs as $k => $v) {
- if (is_array($v)) $v = implode(' ', $v);
- $attrsOut[] = "{$k}=\"{$v}\"";
- }
- if (in_array($html->tag, array('select', 'textarea'))) {
- $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
- $out .= $html->cnt;
- $out .= '</' . $html->tag . '>';
- } else {
- $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
- }
- if (in_array('date', $html->_params)) {
- $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
- <span class="glyphicon glyphicon-calendar"></span>
- </span>
- </div>';
- }
- else if (in_array('time', $html->_params)) {
- $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
- <span class="glyphicon glyphicon-time"></span>
- </span>
- </div>';
- }
- if (true == V::get('appendBack', '', $params)
- && !in_array('date', $html->_params)
- && !in_array('time', $html->_params)
- ) {
- if ($html->tag == 'input' && $taskPerm == 'W') {
- $out = '<div class="input-group show-last-value">' . $out . '<span class="input-group-addon button-appendBack" title="' . htmlspecialchars($fValue) . '"><span class="glyphicon glyphicon-arrow-left"></span></span></div>';
- }
- }
- if ($typeSpecial) {
- $tsParams = array();
- $tsValue = V::get('typespecialValue', '', $params);
- if (!empty($tsValue)) {
- $tsParams['typespecialValue'] = $tsValue;
- }
- $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
- }
- return $out;
- }
- /**
- * List table ids by database
- *
- *
- */
- public static function GetTablesByDbId($db) {
- DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
- static $_cache;
- $return=array();
- if (!$_cache) $_cache = array();
- if (!empty($_SESSION['TableAcl_cache'])) {
- foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
- //if($obj->db==$db)
- $return[$obj['name']]=$tableID;
-
- }
- return $return;
- }
- return null;
- }
-
- /**
- * Get column object. Not initialize
- * @returns object - column instance if exists else null
- *
- * static
- */
- public static function getInstance($tableID) {
- static $_cache;
- if (!$_cache) $_cache = array();
- if (array_key_exists($tableID, $_cache)) {
- return $_cache[$tableID];
- }
- if (!empty($_SESSION['TableAcl_cache'][$tableID])) {
- $obj = new TableAcl($tableID);
- $obj->fromArray($_SESSION['TableAcl_cache'][$tableID]);
- $_cache[$tableID] = $obj;
- return $_cache[$tableID];
- }
- return null;
- }
- public function init($force = false) {
- if ($this->isInitialized() && $force == false) {
- return;
- }
- $dbID = $this->getDB();
- $tblName = $this->getName();
- $db = DB::getDB($dbID);
- if (!$db) {
- die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
- }
- $res = $db->query("show fields from `{$tblName}` ");
- while ($h = $db->fetch_row($res)) {
- $fieldName = $h[0];
- $fieldType = $h[1];
- $this->_types[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
- }
- uasort($this->_fields, array($this, 'sortFieldsCallback'));
- $this->_fixDateFields();
- $this->_sortEnumFields();
- $this->_fixProjectType();
- $fieldIds = array_keys($this->_fields);
- Lib::loadClass('Typespecial');
- $vColsIdList = Typespecial::initFields($fieldIds);
- if (!empty($vColsIdList)) {
- $this->_virtualFieldsIdList = $vColsIdList;
- }
- $this->save();
- }
- private function _fixProjectType() {
- $tblName = $this->getName();
- $fldName = 'M_DIST_TYPE';
- if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
- foreach ($this->_fields as $kFldId => $vFld) {
- if ($vFld['name'] == $fldName) {
- $sqlTypes = array();
- if (!empty($this->_types[$fldName])) {
- if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
- $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
- }
- }
- if (!empty($sqlTypes)) {
- $allowedTypes = array();
- $db = DB::getDB();
- $sql = "select z.DESC
- from `CRM_LISTA_ZASOBOW` as z
- where z.`A_STATUS`='NORMAL'
- and z.`PARENT_ID`={$kFldId}
- order by z.`DESC` asc
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- if (in_array($r->DESC, $sqlTypes)) {
- $allowedTypes[] = $r->DESC;
- }
- }
- sort($allowedTypes);
- if (!empty($allowedTypes)) {
- $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
- }
- }
- }
- }
- }
- }
- private function _sortEnumFields() {
- foreach ($this->_fields as $kFldId => $vFld) {
- $type = $this->getFieldTypeById($kFldId);
- if (!empty($type['type'])) {
- if (substr($type['type'], 0, 4) == 'enum') {
- $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
- if (!empty($sqlTypes)) {
- sort($sqlTypes);
- $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
- }
- }
- }
- }
- }
- private function _fixDateFields() {
- foreach ($this->_types as $kFldName => $vType) {
- if ($kFldName == 'L_APPOITMENT_DATE') {
- $this->_types[$kFldName]['type'] = 'datetime';
- } else if ($kFldName == 'A_PROBLEM_DATE') {
- $this->_types[$kFldName]['type'] = 'datetime';
- }
- }
- }
- public function getUniqueKeys() {
- $sqlKeys = array();
- $dbID = $this->getDB();
- $tblName = $this->getName();
- $db = DB::getDB($dbID);
- if (!$db) {
- die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
- }
- $sql = "SHOW KEYS FROM `{$tblName}`";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- if ($r->Non_unique == '0') {
- $sqlKeys[$r->Column_name] = true;
- }
- }
- $sqlKeys = array_keys($sqlKeys);
- return $sqlKeys;
- }
- public function sortFieldsCallback($a, $b) {
- if ($a['name'] == 'ID') {
- return -1;
- }
- else if ($b['name'] == 'ID') {
- return 1;
- }
- else if ($a['sort_prio'] < $b['sort_prio']) {
- return -1;
- }
- else if ($a['sort_prio'] > $b['sort_prio']) {
- return 1;
- }
- else {
- return 0;
- }
- }
- public function isInitialized() {
- return (!empty($this->_types));
- }
- /**
- * Save data in session cache.
- */
- function save() {
- $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
- }
- public function getFieldTypeById($fieldID) {
- if (!array_key_exists($fieldID, $this->_fields)) {
- return null;
- }
- $colName = $this->_fields[$fieldID]['name'];
- if (!array_key_exists($colName, $this->_types)) {
- return null;
- }
- return $this->_types[$colName];
- }
- public function getFieldType($colName) {
- if (!array_key_exists($colName, $this->_types)) {
- return null;
- }
- return $this->_types[$colName];
- }
- public function hasFieldType($colName) {
- if (array_key_exists($colName, $this->_types)) {
- return true;
- }
- return false;
- }
- public function getVisibleFieldList() {
- $cols = array();
- $id = 0;
- foreach ($this->_fields as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- $id = $kFieldID;
- }
- }
- $cols[$id] = 'ID';
- foreach ($this->_fields as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- continue;
- }
- $cols[$kFieldID] = $vField['name'];
- }
- return $cols;
- }
- public function getExportFieldList() {
- $cols = array();
- $realFlds = $this->getRealFieldList();
- foreach ($realFlds as $vFieldName) {
- $fldId = $this->getFieldIdByName($vFieldName);
- if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
- $cols[] = $vFieldName;
- }
- }
- return $cols;
- }
- /**
- * List of real fields in database.
- */
- public function getRealFieldList() {
- $cols = array();
- $cols[] = 'ID';
- foreach ($this->_fields as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- continue;
- }
- if (array_key_exists($vField['name'], $this->_types)) {
- $cols[] = $vField['name'];
- }
- }
- return $cols;
- }
- public function getVirtualFieldList() {
- $cols = array();
- foreach ($this->_fields as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- continue;
- }
- if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
- $cols[$kFieldID] = $vField['name'];
- }
- else if (!array_key_exists($vField['name'], $this->_types)) {
- $cols[$kFieldID] = $vField['name'];
- }
- }
- return $cols;
- }
- public function getFieldLabel($fieldID) {
- if (array_key_exists($fieldID, $this->_fields)) {
- if (!empty($this->_fields[$fieldID]['label'])) {
- return $this->_fields[$fieldID]['label'];
- }
- }
- return null;
- }
- public function getFieldOpis($fieldID) {
- if (array_key_exists($fieldID, $this->_fields)) {
- if (!empty($this->_fields[$fieldID]['opis'])) {
- return $this->_fields[$fieldID]['opis'];
- }
- }
- return null;
- }
- public function getTypes() {
- return $this->_types;
- }
- public function fixEmptyValueFromUser($fieldID) {
- $value = '';
- $type = $this->getFieldTypeById($fieldID);
- if ($type) {
- if ($type['type'] == 'date') {
- $value = $type['default'];
- }
- if (substr($type['type'], 0, 3) == 'int'
- || substr($type['type'], 0, 7) == 'tinyint'
- || substr($type['type'], 0, 8) == 'smallint'
- || substr($type['type'], 0, 6) == 'bigint'
- ) {
- $value = intval($type['default']);
- }
- // fix bug when field is unique and is null allowed: change empty string to null
- if ($type['null']) {
- $value = 'NULL';
- }
- // fix bug when field is enum and is set to '0': for php '0' is empty
- if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
- if (false !== strpos($type['type'], "''")) {
- // enum('', '1','2')
- $value = '';
- } else if (false !== strpos($type['type'], "'0'")) {
- // enum('0', '1','2')
- $value = '0';
- } else {
- $value = $type['default'];
- }
- }
- }
- return $value;
- }
- public function fromArray($arr) {
- $this->_db = $arr['db'];
- $this->_name = $arr['name'];
- $this->_label = $arr['label'];
- $this->_opis = $arr['opis'];
- $this->_fields = $arr['fields'];
- $this->_virtualFieldsIdList = $arr['virtualFieldsIdList'];
- $this->_types = $arr['types'];
- }
- public function toArray() {
- $arr = array();
- $arr['db'] = $this->_db;
- $arr['name'] = $this->_name;
- $arr['label'] = $this->_label;
- $arr['opis'] = $this->_opis;
- $arr['fields'] = $this->_fields;
- $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
- $arr['types'] = $this->_types;
- return $arr;
- }
- public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
- $obj = new stdClass();
- $fields = $this->getFields();
- foreach ($fields as $kID => $vField) {
- if (!$this->isAllowed($kID, 'C')) {
- continue;
- }
- if (array_key_exists("f{$kID}", $args)) {
- $obj->{$vField['name']} = $args["f{$kID}"];
- if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
- $obj->{$vField['name']} = $this->fixEmptyValueFromUser($kID);
- }
- }
- }
- {// add DefaultAclGroup if no create perms ('C')
- $defaultAclGroup = User::getDefaultAclGroup();
- if ($defaultAclGroup) {
- foreach ($fields as $kID => $vField) {
- if (!$this->isAllowed($kID, 'C')) {
- if ($vField['name'] == 'A_ADM_COMPANY') {
- $obj->{$vField['name']} = $defaultAclGroup;
- }
- else if ($vField['name'] == 'A_CLASSIFIED') {
- $obj->{$vField['name']} = $defaultAclGroup;
- }
- }
- }
- }
- }
- return $obj;
- }
- public function getItem($id) {
- $ds = $this->getDataSource();
- return $ds->getItem($id);
- }
- public function addItem($item) {
- $ds = $this->getDataSource();
- return $ds->addItem($item);
- }
- public function getExportDataSource($cols = array()) {
- $exportFieldList = $this->getExportFieldList();
- if (!empty($cols)) {
- $fltrExportFlds = array();
- foreach ($exportFieldList as $fldName) {
- if (in_array($fldName, $cols)) {
- $fltrExportFlds[] = $fldName;
- }
- }
- $exportFieldList = $fltrExportFlds;
- }
- $dataSource = $this->_getDataSource($exportFieldList);
- return $dataSource;
- }
- public function getDataSource() {
- $realFieldList = $this->getRealFieldList();
- $dataSource = $this->_getDataSource($realFieldList);
- $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
- $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
- $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
- foreach ($adminFields as $vAdmFld) {
- if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
- $dataSource->addCol($vAdmFld);
- }
- }
- return $dataSource;
- }
- private function _getDataSource($cols) {
- Lib::loadClass('Data_Source');
- $dataSource = new Data_Source($this->getDB());
- $dataSource->setTable($this->getName());
- $dataSource->setCols($cols);
- $dataSource->setColTypes($this->getTypes());
- $dataSource->setVirtualCols($this->getVirtualFieldList());
- $dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
- return $dataSource;
- }
- }
|