| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942 |
- <?php
- Lib::loadClass('Core_AclBase');
- Lib::loadClass('SchemaException');
- Lib::loadClass('Typespecial');
- Lib::loadClass('ProcesHelper');
- Lib::loadClass('Schema_TableFactory');
- Lib::loadClass('DataSourceFactory');
- /**
- * $_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 extends Core_AclBase {
- public $_rootTableName = null;
- public $_zasobID = '';
- public $_db = '';
- public $_name = '';
- public $_label = '';
- public $_opis = '';
- public $_executed_fieldsInit = null;
- public $_fields = array();
- public $_types = array();
- public $_virtualFieldsIdList = array();
- public $_schemaLoaded = false;
- public function __construct($zasobID) { $this->_zasobID = $zasobID; }
- public function getNamespace() { return 'default_db/' . $this->getName(); }
- public function getRootNamespace() { return 'default_db/' . $this->getName(); }
- public function getSourceName() { return 'default_db'; }
- public function getName() { return $this->_name; }
- public function getRootTableName() {
- if (empty($this->_name)) return null;// throw new Exception("Table name not defined");
- if ($this->_rootTableName) return $this->_rootTableName;
- if (!$this->_db) return null;// throw new Exception("Database not defined in table {$this->_zasobID}");
- DBG::simpleLog('auth', "TableAcl::getRootTableName(name: '{$this->_name}')");
- $dbName = DB::getPDO($this->_db)->getDatabaseName();
- $this->_rootTableName = DB::getPDO()->fetchValue("
- select t.TABLE_NAME
- from `information_schema`.`TABLES` t
- where t.TABLE_SCHEMA = '{$dbName}'
- and t.TABLE_NAME LIKE '{$this->_name}'
- ");
- if (!$this->_rootTableName) return null;// throw new Exception("Table '{$this->_name}' not exists!");
- if ($this->_rootTableName != $this->_name) {
- if ($this->_zasobID) {
- $affected = DB::getPDO()->update('CRM_LISTA_ZASOBOW', 'ID', $this->_zasobID, [
- 'DESC' => $this->_rootTableName
- ]);
- DBG::log("TableAcl({$this->_zasobID})->getRootTableName fixed zasob table name (\$affected={$affected})");
- }
- $this->_name = $this->_rootTableName;
- $this->save();
- }
- return $this->_rootTableName;
- }
- public function getID() { return $this->_zasobID; }
- public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
- public function setName($name) {// TODO: used only by setNameByTableId
- DBG::log("setName('{$name}')");
- $this->_name = $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 setNameByTableId($tableID) {
- //used for init without knowing table name
- $db = DB::getDB();
- $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 getRawOpis() {
- $opis = $this->_opis;
- if (!$opis) return $this->getRawLabel(100);
- $opis = strip_tags($opis);
- return $opis;
- }
- 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 getLongRawLabel($posLimit = 30) {
- $longLabel = $this->getRawLabel($posLimit);
- $opis = $this->_opis;
- if ($longLabel != $this->_name) {
- $longLabel .= ' ' . $this->_name;
- }
- 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->getFields()[$fieldID];
- }
- public function hasFieldById($fieldID) {
- return array_key_exists($fieldID, $this->getFields());
- }
- public function getFields() {
- $this->fieldsInit();
- return $this->_fields;
- }
- public function setFieldPerms($fieldID, $perms) {
- if (array_key_exists($fieldID, $this->getFields())) {
- $this->_fields[$fieldID]['perms'] .= $perms;
- }
- }
- public function getFieldPerms($fieldID) {
- if (array_key_exists($fieldID, $this->getFields())) {
- $perms = V::get('perms', '', $this->_fields[$fieldID]);
- if ($perms) {
- return implode(',', array_unique(str_split($perms)));
- }
- }
- return '';
- }
- public function hasFieldPerm($fieldID, $perm) {// TODO: legacy
- if (array_key_exists($fieldID, $this->getFields())) {
- 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->getFields() as $kID => $vField) {
- if ($vField['name'] == $fieldName) {
- $fieldID = $kID;
- }
- }
- return $fieldID;
- }
- public function hasEditPerms() {
- foreach ($this->getFields() as $kFldID => $vFld) {
- if ($this->hasFieldPerm($kFldID, 'W')) return true;
- if ($this->hasFieldPerm($kFldID, 'C')) return true;
- if ($this->hasFieldPerm($kFldID, 'S')) return true;
- }
- return false;
- }
- public function hasSuperAccessPerms() {
- foreach ($this->getFields() 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->getFields() as $kFldID => $vFld) {
- if ($this->hasFieldPerm($kFldID, 'S')) {
- return true;
- }
- }
- return false;
- }
- /**
- * @param $taskPerm - 'C', 'W', 'R'
- */
- public function isAllowed($fieldID, $taskPerm, $record = null) {// TODO: legacy - replace with canWriteField, canReadField, canWriteObjectField, canReadObjectField, canCreateField
- 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->getFields()[$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
- ,'fieldPerms'=>V::get('perms', null, V::get($fieldID, null, $this->_fields))
- ,'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;
- }
- public function canCreateField($fieldName) {
- $idZasob = $this->getFieldIdByName($fieldName);
- return ($idZasob && $this->hasFieldPerm($idZasob, $taskPerm = 'C'));
- }
- public function canReadField($fieldName) {
- $idZasob = $this->getFieldIdByName($fieldName);
- if (!$idZasob) return false;
- if ('ID' == $fieldName) return true;
- if ('A_RECORD_CREATE_DATE' == $fieldName) return true;
- if ('A_RECORD_CREATE_AUTHOR' == $fieldName) return true;
- if ('A_RECORD_UPDATE_DATE' == $fieldName) return true;
- if ('A_RECORD_UPDATE_AUTHOR' == $fieldName) return true;
- if ($this->hasFieldPerm($idZasob, $taskPerm = 'R')) return true;
- if ($this->hasFieldPerm($idZasob, $taskPerm = 'V')) return true;
- return false;
- }
- public function canReadObjectField($fieldName, $record) {
- // if (!$this->canReadField($fieldName)) return false;
- if ('ID' == $fieldName) return true;
- if ('A_RECORD_CREATE_DATE' == $fieldName) return true;
- if ('A_RECORD_CREATE_AUTHOR' == $fieldName) return true;
- if ('A_RECORD_UPDATE_DATE' == $fieldName) return true;
- if ('A_RECORD_UPDATE_AUTHOR' == $fieldName) return true;
- if (!$record) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">canReadObjectField('.$fieldName.', $record) - record not set - return false (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): '.var_export($record, true).'</pre>';}
- return false;
- }
- $idZasob = $this->getFieldIdByName($fieldName);
- if (!$idZasob) {
- if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">canReadObjectField('.$fieldName.', $record) - $idZasob not set - return false (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): '.var_export($record, true).'</pre>';}
- return false;
- }
- if (!$this->hasFieldPerm($idZasob, $taskPerm = 'R')) {
- if ($this->hasFieldPerm($idZasob, 'V')) return true;
- return ($this->hasFieldPerm($idZasob, 'O') && ($this->canReadRecord($record) || $this->canWriteRecord($record)));// 'WO' or 'CO'
- }
- return ($this->canReadRecord($record) || $this->hasFieldPerm($idZasob, 'V'));// 'R' require
- }
- public function canWriteField($fieldName) {
- $idZasob = $this->getFieldIdByName($fieldName);
- if (!$idZasob) return false;
- return $this->hasFieldPerm($idZasob, $taskPerm = 'W');
- }
- public function canWriteObjectField($fieldName, $record) {
- if (!$this->canWriteField($fieldName)) return false;
- $idZasob = $this->getFieldIdByName($fieldName);
- if (!$idZasob) return false;
- if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: "'.$fieldName.'" ['.$idZasob.'], canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($idZasob, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($idZasob, 'W') . '))';}
- return ($this->canWriteRecord($record) || $this->hasFieldPerm($idZasob, 'S'));
- }
- /**
- * @param $taskPerm - 'C', 'W'
- */
- public function getFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
- $out = '';
- if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
- if ($taskPerm == 'R') throw new Exception("Brak uprawnień do odczytu");
- else if ($taskPerm == 'W') throw new Exception("Brak uprawnień do zapisu");
- else throw new Exception("Brak uprawnień do tego pola ({$taskPerm})");
- }
- $colName = $this->getFields()[$fieldID]['name'];
- if ($colName == 'ID') return;
- $colType = $this->getFieldTypeById($fieldID);
- if (!$colType) throw new Exception("Error - unknown type");
- $item = array();
- $item['htmlType'] = '';
- $item['value'] = $fValue;
- $item['canRead'] = $this->isAllowed($fieldID, 'R', $record);
- $item['canWrite'] = $this->isAllowed($fieldID, 'W', $record);
- $item['rawColType'] = $colType;
- return $item;
- $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}";
- }
- */
- }
- if ($maxLength > 255) {// Fix for long varchar - use textarea
- $html->tag = 'textarea';
- $html->cnt = htmlspecialchars($fValue);
- $html->attrs['rows'] = '3';
- unset($html->attrs['type']);
- unset($html->attrs['value']);
- }
- }
- 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 ($colType['type'] == 'timestamp') {
- $testDatePicker = true;
- if ($testDatePicker) {
- $html->attrs['type'] = 'text';
- $html->_params[] = 'date';
- $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
- $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
- $html->attrs['maxlength'] = 19;
- if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
- $html->attrs['value'] = '';
- }
- } else {
- $html->attrs['type'] = 'date';
- }
- }
- 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 throw new Exception("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>';
- }
- }
- $typeSpecial = Typespecial::getInstance($fieldID, $colName);
- if ($typeSpecial) {
- throw new Exception("TODO: 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;
- }
- /**
- * @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->getFields()[$fieldID]['name'];
- if ($colName == 'ID') {
- return $out;
- }
- $colType = $this->getFieldTypeById($fieldID);
- if (!$colType) {
- $out .= 'Error - unknown type';
- return $out;
- }
- $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}";
- }
- */
- }
- if ($maxLength > 255) {// Fix for long varchar - use textarea
- $html->tag = 'textarea';
- $html->cnt = htmlspecialchars($fValue);
- $html->attrs['rows'] = '3';
- unset($html->attrs['type']);
- unset($html->attrs['value']);
- }
- }
- 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 ($colType['type'] == 'timestamp') {
- $testDatePicker = true;
- if ($testDatePicker) {
- $html->attrs['type'] = 'text';
- $html->_params[] = 'date';
- $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
- $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
- $html->attrs['maxlength'] = 19;
- if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
- $html->attrs['value'] = '';
- }
- } else {
- $html->attrs['type'] = 'date';
- }
- }
- 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($idTable) {
- static $_cache;
- if (!$_cache) $_cache = array();
- if (array_key_exists($idTable, $_cache)) {
- return $_cache[$idTable];
- }
- if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
- $tableAcl = new TableAcl($idTable);
- $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
- $_cache[$idTable] = $tableAcl;
- return $_cache[$idTable];
- }
- return null;
- }
- public static function buildInstance($idTable, $tableConfig) {
- static $_cache;
- if (!$_cache) $_cache = array();
- if (array_key_exists($idTable, $_cache)) {
- return $_cache[$idTable];
- }
- if (empty($tableConfig)) throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
- // if (empty($tableConfig)) {
- // $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
- // if (!$zasobObj) {
- // return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
- // }
- // $tableConfig['db'] = $zasobObj->P__ID;
- // $tableConfig['name'] = $zasobObj->DESC;
- // $tableConfig['label'] = $zasobObj->DESC_PL;
- // $tableConfig['opis'] = $zasobObj->OPIS;
- //
- // $userAcl = User::getAcl();
- // $userPermsForTable = $userAcl->getPermsForTable($idTable);
- // if (!$userPermsForTable) {
- // return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
- // }
- // echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
- // if(0){// TODO: from UserAcl big query
- // $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
- // $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
- // $tableConfig['fields'];// $this->getFields()
- // $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
- // //$tableConfig['types'];// $this->_types
- // }
- // }
- if (empty($tableConfig)) throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
- $obj = new TableAcl($idTable);
- $obj->fromArray($tableConfig);
- $obj->getRootTableName();
- $obj->save();
- $_cache[$idTable] = $obj;
- return $_cache[$idTable];
- }
- public function loadSchema() {
- if ($this->_schemaLoaded) return (bool)$this->_schemaClass;
- $srvName = $_SERVER['SERVER_NAME'];
- $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
- $this->_schemaLoaded = true;
- return (bool)$this->_schemaClass;
- }
- public function init($force = false) {
- // DBG::log("DBG INIT - init ({$this->_zasobID}, {$this->_rootTableName})");
- }
- public function fieldsInit($force = false) {
- $this->loadSchema();
- if (null !== $this->_executed_fieldsInit) return;
- $this->_executed_fieldsInit = true;
- DBG::log("DBG INIT - fieldsInit ({$this->_zasobID}, {$this->_rootTableName})");
- if (empty($this->_fields)) {
- $this->_types = array();// clear _types @see $this->isInitialized
- $userAcl = User::getAcl();
- $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
- // DBG::log(['msg'=>"INIT({$this->_zasobID})::initFieldsFromConfig \$fieldsConfig", '$fieldsConfig'=>$fieldsConfig]);
- $this->initFieldsFromConfig($fieldsConfig);
- //DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
- }
- if ($this->isInitialized() && $force == false) {
- return;
- }
- DBG::simpleLog('auth', "TableAcl::init(name: '{$this->_name}')");
- // DBG::log(['msg'=>"INIT({$this->_zasobID}):: \$this", '$this'=>$this, 'name'=>$this->getName()]);
- $ds = $this->getDataSource();
- // DBG::log(['msg'=>"INIT({$this->_zasobID})::getDataSource \$ds", '$ds'=>$ds]);
- $this->_types = $ds->getFieldTypes();
- // DBG::log(['msg'=>"INIT({$this->_zasobID})::getFieldTypes \$this->_types", '$this->_types'=>$this->_types]);
- uasort($this->_fields, array($this, 'sortFieldsCallback'));
- $this->_fixTypes();
- // DBG::log(['msg'=>"INIT({$this->_zasobID})::after fixTypes \$this->_types", '$this->_types'=>$this->_types]);
- $this->save();
- }
- public function _fixTypes() {
- $this->_fixDateFields();
- $this->_sortEnumFields();
- $this->_fixProjectType();
- if ($this->loadSchema()) $this->_types = $this->_schemaClass->fixTypes($this->_types);
- //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
- // fixGeomTypesFromSimpleType
- foreach ($this->_types as $fieldName => $field) {
- // [the_geom] => Array(
- // [type] => geometry
- // [null] => 1
- // [default] =>
- // [simpleType] => linestring
- if ('geometry' == $field['type']) {
- if (!empty($field['simpleType'])) {
- switch ($field['simpleType']) {
- case 'gml:LineStringPropertyType': $this->_types[ $fieldName ][ 'type' ] = 'linestring'; break;
- }
- }
- $dbName = DB::getPDO()->getDatabaseName();
- $tableName = $this->getName();
- $geomType = DB::getPDO()->fetchValue("
- select DATA_TYPE
- from `information_schema`.`COLUMNS`
- where TABLE_SCHEMA='{$dbName}'
- and TABLE_NAME='{$tableName}'
- and COLUMN_NAME='{$fieldName}'
- ");
- if (!empty($geomType)) $this->_types[ $fieldName ][ 'type' ] = $geomType;
- }
- }
- {// TODO: replace this by $this->_schemaClass (from DataSourceFactory::build(...))
- $fieldIds = array_keys($this->_fields);
- $vColsIdList = Typespecial::initFields($fieldIds);
- if (!empty($vColsIdList)) {
- $this->_virtualFieldsIdList = $vColsIdList;
- }
- }
- }
- public function initFieldsFromConfig($fieldsConfig) {
- foreach ($fieldsConfig as $idField => $vFieldConfig) {
- if ((int)$idField <= 0) {
- DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
- trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
- continue;
- }
- //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasFieldById($vFieldConfig->ID_CELL).') ';echo'</pre>';
- if (!$this->hasFieldById($vFieldConfig['ID_CELL'])) {
- //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') addFld('.$vFieldConfig['ID_CELL'] . ', ' . $vFieldConfig['CELL_NAME'] . ', ' . $vFieldConfig['CELL_DESC'] . ', ' . $vFieldConfig['SORT_PRIO'] . ', ' . $vFieldConfig['CELL_LABEL'].') ';echo'</pre>';
- $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
- }
- //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasFieldById($vFieldConfig['ID_CELL']).') ';echo'</pre>';
- if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
- $vFieldConfig['FORM_TREAT'] = '';
- if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
- if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
- if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
- if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
- if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
- if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
- if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
- if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
- }
- //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
- if (!empty($vFieldConfig['FORM_TREAT'])) {
- $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
- }
- }
- }
- 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() {// TODO: RM NOT USED?
- $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();
- // DBG::log("save(name='{$_SESSION['TableAcl_cache'][$this->_zasobID]['name']}')");
- // DBG::simpleLog('auth', "TableAcl::save(name: '{$this->_name}')");
- }
- public function getFieldTypeById($fieldID) {
- if (!array_key_exists($fieldID, $this->getFields())) return null;
- $fieldName = $this->_fields[$fieldID]['name'];
- if (!array_key_exists($fieldName, $this->_types)) return null;
- return $this->_types[$fieldName];
- }
- public function getFieldType($fieldName) {
- if (!array_key_exists($fieldName, $this->_types)) return null;
- return $this->_types[$fieldName];
- }
- public function hasFieldType($fieldName) {
- return (array_key_exists($fieldName, $this->_types));
- }
- public function getVisibleFieldList() { return $this->getVisibleFieldListByIdZasob(); }
- public function getVisibleFieldListByIdZasob() {
- $cols = array();
- $id = 0;
- foreach ($this->getFields() as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- $id = $kFieldID;
- }
- }
- $cols[$id] = 'ID';
- foreach ($this->getFields() 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->getFields() as $kFieldID => $vField) {
- if ($vField['name'] == 'ID') {
- continue;
- }
- if (array_key_exists($vField['name'], $this->_types)) {
- $cols[] = $vField['name'];
- }
- }
- return $cols;
- }
- public function getFieldListByIdZasob() {
- $cols = array();
- foreach ($this->getFields() as $idField => $fld) {
- if ($fld['name'] == 'ID') {
- $cols[$idField] = 'ID';
- }
- }
- foreach ($this->getFields() as $idField => $fld) {
- if ($fld['name'] == 'ID') continue;
- $cols[$idField] = $fld['name'];
- }
- return $cols;
- }
- public function getRealFieldListByIdZasob() {
- $cols = array();
- foreach ($this->getFields() as $idField => $fld) {
- if ($fld['name'] == 'ID') {
- $cols[$idField] = 'ID';
- }
- }
- foreach ($this->getFields() as $idField => $fld) {
- if ($fld['name'] == 'ID') continue;
- if (array_key_exists($fld['name'], $this->_types)) {
- $cols[$idField] = $fld['name'];
- }
- }
- return $cols;
- }
- public function getVirtualFieldListByIdZasob() {
- $cols = array();
- foreach ($this->getFields() 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($fieldName) {
- $idField = $this->getFieldIdByName($fieldName);
- if (array_key_exists($idField, $this->getFields())) {
- if (!empty($this->_fields[$idField]['label'])) {
- return $this->_fields[$idField]['label'];
- }
- }
- return null;
- }
- public function getFieldOpis($fieldName) {
- $idField = $this->getFieldIdByName($fieldName);
- if (array_key_exists($idField, $this->getFields())) {
- if (!empty($this->_fields[$idField]['opis'])) {
- return $this->_fields[$idField]['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) {
- // DBG::log("fromArray(name='{$arr['name']}')");
- $this->_db = $arr['db'];
- $this->_name = $arr['name'];
- $this->_rootTableName = V::get('_rootTableName', null, $arr);
- $this->_label = $arr['label'];
- $this->_opis = $arr['opis'];
- $this->_executed_fieldsInit = V::get('_executed_fieldsInit', null, $arr);
- $this->_fields = V::get('fields', array(), $arr);
- $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
- $this->_types = V::get('types', array(), $arr);
- }
- public function toArray() {
- $arr = array();
- $arr['db'] = $this->_db;
- $arr['name'] = $this->_name;
- $arr['_rootTableName'] = $this->_rootTableName;
- $arr['label'] = $this->_label;
- $arr['opis'] = $this->_opis;
- if ($this->_executed_fieldsInit) $arr['_executed_fieldsInit'] = $this->_executed_fieldsInit;
- $arr['fields'] = $this->_fields;
- $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
- $arr['types'] = $this->_types;
- return $arr;
- }
- public function convertObjectFromUserInput($userItem, $type = 'array_by_id', $prefix = 'f') {// TODO: rename / Legacy
- $item = $this->parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f');
- foreach ($item as $fieldName => $value) {
- $item[$fieldName] = $this->validateAndFixField($fieldName, $value);
- }
- DBG::log(['userItem' => $userItem, 'item' => $item], '', "after parseUserItem, validateAndFixField");
- return $item;
- }
- public function parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f') {
- $item = [];
- foreach ($this->getFieldsMap() as $userKey => $fieldName) {
- if (!array_key_exists("f{$userKey}", $userItem)) continue;
- $item[$fieldName] = $userItem["f{$userKey}"];
- }
- return $item;
- }
- public function getFieldsMap() {// [ $userKey => $fieldName ] // TODO: $this->_fieldsMap
- $fieldsMap = [];
- foreach ($this->getFields() as $id => $field) $fieldsMap[ $id ] = $field['name'];
- return $fieldsMap;
- }
- public function validateAndFixField($fieldName, $value) {
- if (empty($value) && 0 === strlen($value)) {// TODO: fixEmptyValueFromUser
- return $this->fixFieldEmptyValue($fieldName);
- }
- $xsdType = $this->getXsdFieldType($fieldName);
- switch ($xsdType) {
- case 'xsd:decimal': {
- return str_replace([',', ' '], ['.', ''], $value);
- } break;
- case 'p5:price': {
- return V::convert($value, 'price');
- } break;
- }
- return $value;
- }
- public function fixFieldEmptyValue($fieldName) {// TODO: legacy
- $value = '';
- $type = $this->getFieldType($fieldName);
- if (!$type) return '';
- if ('date' == $type['type']) {
- $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 buildQuery($params = []) {
- Lib::loadClass('AclQueryFeatures');
- return new AclQueryFeatures($this, $params);
- }
- public function getItem($primaryKey, $params = []) {
- $ds = $this->getDataSource();
- return $ds->getItem($primaryKey);
- }
- public function prepareQuery($params = array()) {
- DBG::log($params, 'array', 'TableAcl->prepareQuery $params');
- $ds = $this->getDataSource();
- $filtrIsInstance = []; // TODO: read instances from $params
- $filtrIsNotInstance = [];
- Lib::loadClass('ACL');
- return ACL::query($this)
- ->isInstance($filtrIsInstance)
- ->isNotInstance($filtrIsNotInstance)
- ->whereRaw($ds->_parseSqlWhere($params));
- }
- public function getItems($params = array()) {
- $orderBy = V::get('order_by', '', $params);
- $orderDir = V::get('order_dir', '', $params);
- $sortBy = (!empty($orderBy))
- ? ( !empty($orderDir) ? "{$orderBy} {$orderDir}" : $orderBy )
- : '';
- $limit = V::get('limit', 10, $params, 'int');
- $offset = V::get('limitstart', 0, $params, 'int');
- $ds = $this->getDataSource();
- return $this->prepareQuery($params)
- ->select([
- 'rawSelect' => $ds->_getSqlCols()
- ])
- ->select(!empty($params['@instances']) ? '@instances' : '')
- ->limit($limit)
- ->offset($offset)
- ->orderBy($sortBy)
- ->fetchAll();
- }
- public function getTotal($params = array()) {
- return $this->prepareQuery($params)
- ->select([ 'rawSelect' => "count(1) as cnt" ])
- ->fetchValue();
- }
- public function getColDefault($fieldName) {
- $ds = $this->getDataSource();
- return $ds->getColDefault($fieldName);
- }
- public function getSpecialFilters() {
- $ds = $this->getDataSource();
- return $ds->getSpecialFilters();
- }
- public function getGeomFields() {
- $ds = $this->getDataSource();
- return $ds->getGeomFields();
- }
- public function isGeomField($fldName) {
- $ds = $this->getDataSource();
- return $ds->isGeomField($fldName);
- }
- public function getGeomFieldType($fieldName) {
- $dbGeomType = $this->getFieldType($fieldName);
- $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
- $geomType = strtolower($dbGeomType);
- return $geomType;
- }
- public function getHistItems($primaryKey) {
- if ($this->loadSchema() && method_exists($this->_schemaClass, 'hasGetHistItems') && $this->_schemaClass->hasGetHistItems()) {
- return $this->_schemaClass->getHistItems($primaryKey);
- }
- $ds = $this->getDataSource();
- return $ds->getHistItems($primaryKey);
- }
- public function getHistItem($id, $idHist) {
- $ds = $this->getDataSource();
- $histItems = $ds->getHistItems($id, ['ID' => $idHist]);
- if (empty($histItems)) return null;
- return reset($histItems);
- }
- public function addItem($itemTodo) {
- if (is_object($itemTodo)) {
- $itemTodo = (array)$itemTodo;
- } else if (!is_array($itemTodo)) {
- throw new HttpException('Item is not array', 400);
- }
- $ds = $this->getDataSource();
- // from convertObjectFromUserInput - fixEmptyValueFromUser
- $item = array();
- $fields = $this->getFields();
- foreach ($fields as $kID => $vField) {
- $vFieldName = $vField['name'];
- if (!$this->isAllowed($kID, 'C')) {
- continue;
- }
- if (isset($itemTodo[$vFieldName])) {
- $value = $itemTodo[$vFieldName];
- if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
- $value = $this->fixEmptyValueFromUser($kID);
- }
- $item[$vFieldName] = $value;
- }
- }
- // add empty record is allowed - empty($item)
- {// add DefaultAclGroup if no create perms ('C')
- $defaultAclGroup = User::getDefaultAclGroup();
- if ($defaultAclGroup) {
- $permFields = array('A_ADM_COMPANY', 'A_CLASSIFIED');
- foreach ($permFields as $permFldName) {
- if (array_key_exists($permFldName, $this->_types)) {
- $permFldId = $this->getFieldIdByName($permFldName);
- if (0 == $permFldId || !$this->isAllowed($permFldId, 'C')) {
- $item[$permFldName] = $defaultAclGroup;
- }
- }
- }
- }
- }
- DBG::_('DBG_DS', '>2', "addItem", $item, __CLASS__, __FUNCTION__, __LINE__);
- return $ds->addItem($item);
- }
- /**
- * @param array $itemPatch
- */
- public function updateItem($itemPatch) {
- if (is_object($itemPatch)) {
- $itemPatch = (array)$itemPatch;
- } else if (!is_array($itemPatch)) {
- throw new HttpException('Item patch is not array', 400);
- }
- if (empty($itemPatch)) {
- //throw new Exception('Item patch is empty');
- return 0;// nothing to change
- }
- $primaryKeyField = $this->getPrimaryKeyField();
- if (empty($itemPatch[$primaryKeyField])) throw new HttpException("Item Primary Key not set!", 400);
- $primaryKey = $itemPatch[$primaryKeyField];
- $itemOld = $this->getItem($primaryKey);
- if (!$itemOld) throw new HttpException("Item not exists!", 404);
- if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) throw new HttpException("Brak dostępu do rekordu", 403);
- // $itemPatch from user input to $itemPatchChecked
- $itemPatchChecked = array();
- $fields = $this->getFields();
- foreach ($fields as $kID => $vField) {
- $vFieldName = $vField['name'];
- if (!$this->isAllowed($kID, 'W', $itemOld)) {
- continue;
- }
- if (array_key_exists($vFieldName, $itemPatch)) {
- if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
- // default value for perms 'W' without 'R' is '*****'
- }
- else {
- $value = $itemPatch[$vFieldName];
- if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
- $value = $this->fixEmptyValueFromUser($kID);
- }
- if ($value != $itemOld->$vFieldName) {
- $itemPatchChecked[$vFieldName] = $value;
- }
- }
- }
- }
- if (empty($itemPatchChecked)) {
- //throw new HttpException("Item Primary Key not set!", 400);
- return 0;// nothing to change
- }
- $itemPatchChecked[$primaryKeyField] = $primaryKey;
- if ($this->loadSchema() && method_exists($this->_schemaClass, 'hasUpdateItem') && $this->_schemaClass->hasUpdateItem()) {
- $affected = $this->_schemaClass->updateItem($itemPatchChecked);
- } else {
- $ds = $this->getDataSource();
- $affected = $ds->updateItem($itemPatchChecked);
- }
- return $affected;
- }
- public function createItemCopy($item) {
- $ds = $this->getDataSource();
- $types = $this->getTypes();
- $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
- $primaryKeyField = $ds->getPrimaryKeyField();
- $itemCopy = array();
- foreach ($types as $kName => $vType) {
- if ($kName == $primaryKeyField) continue;
- if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) continue;
- $value = V::get($kName, '', $item);
- if (in_array($kName, $uniqKeys)) {
- $value .= '?';
- }
- if ($ds->isGeomField($kName) && !empty($value)) {
- $value = "GeomFromText('{$value}')";
- }
- $itemCopy[$kName] = $value;
- }
- return $itemCopy;
- }
- 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'));
- $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
- $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) {
- $dsConfig = array();
- $dsConfig['source_id'] = $this->getDB();
- $dsConfig['object_name'] = $this->getRootTableName();
- $dsConfig['fields'] = $cols;
- $dsConfig['field_types'] = $this->getTypes();
- $dsConfig['fields_virtual'] = $this->getVirtualFieldListByIdZasob();
- $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms(); // filtr Access is visible only if user dont have supe access perms. If has then see all rows
- return DataSourceFactory::buildFromZasobInfo($dsConfig);
- }
- public function getPrimaryKeyField() {
- if ($this->loadSchema()) {
- $pkField = $this->_schemaClass->getPrimaryKeyField();
- if ($pkField) return $pkField;
- }
- $ds = $this->getDataSource();
- return $ds->getPrimaryKeyField();
- }
- public function isIntegerField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 3) == 'int'
- || substr($type['type'], 0, 7) == 'tinyint'
- || substr($type['type'], 0, 8) == 'smallint'
- || substr($type['type'], 0, 9) == 'mediumint'
- || substr($type['type'], 0, 6) == 'bigint'
- ) {
- return true;
- }
- return false;
- }
- public function isDecimalField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 7) == 'decimal'
- || substr($type['type'], 0, 7) == 'numeric'
- || substr($type['type'], 0, 6) == 'double'
- || substr($type['type'], 0, 5) == 'float'
- || substr($type['type'], 0, 4) == 'real'
- ) {
- return true;
- }
- return false;
- }
- public function isDateField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 4) == 'date' && substr($type['type'], 0, 8) != 'datetime') {
- return true;
- }
- return false;
- }
- public function isDateTimeField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 8) == 'datetime') {
- return true;
- }
- return false;
- }
- public function isStringField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 7) == 'varchar'
- || substr($colType['type'], 0, 4) == 'char'
- ) {
- return true;
- }
- return false;
- }
- public function isTextField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 4) == 'text'
- || substr($type['type'], 0, 8) == 'tinytext'
- || substr($type['type'], 0, 10) == 'mediumtext'
- || substr($type['type'], 0, 8) == 'longtext'
- ) {
- return true;
- }
- return false;
- }
- public function isBinaryField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 4) == 'blob'
- || substr($type['type'], 0, 8) == 'tinyblob'
- || substr($type['type'], 0, 10) == 'mediumblob'
- || substr($type['type'], 0, 8) == 'longblob'
- ) {
- return true;
- }
- return false;
- }
- public function isEnumerationField($fldName) {
- $type = $this->getFieldType($fldName);
- if (!$type) return false;
- if (substr($type['type'], 0, 4) == 'enum') {
- return true;
- }
- return false;
- }
- public function getEnumerations($fldName) {
- $enum = array();
- $type = $this->getFieldType($fldName);
- if (!$type) return $enum;
- if (!$this->isEnumerationField($fldName)) return $enum;
- $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
- foreach ($values as $val) {
- $enum[$val] = $val;
- }
- return $enum;
- }
- public function getAttributesFromZasoby() {
- $attributes = array();
- $db = DB::getDB();
- $sql = "
- select `ID` as `id_zasob`
- , `DESC` as `field_name`
- , `DESC_PL` as `label`
- , `OPIS` as `description`
- from `CRM_LISTA_ZASOBOW`
- where `PARENT_ID`={$this->_zasobID}
- and `TYPE`='KOMORKA'
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $fldAttr = array();
- $fldAttr['id_zasob'] = $r->id_zasob;
- if (!empty($r->label)) $fldAttr['label'] = $r->label;
- if (!empty($r->description)) $fldAttr['description'] = $r->description;
- $attributes[$r->field_name] = $fldAttr;
- }
- // fetch field values
- $sql = "
- select z.`ID` as `id_zasob`
- , z.`DESC` as `field_name`
- , v.`DESC` as `value`
- , v.`DESC_PL` as `label`
- -- , v.`OPIS` as `description`
- from `CRM_LISTA_ZASOBOW` z
- join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
- where z.`PARENT_ID`={$this->_zasobID}
- and z.`TYPE`='KOMORKA'
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- if (!empty($r->value) && !empty($r->label)) {
- $valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
- $valuesMap[$r->value] = $r->label;
- $attributes[$r->field_name]['valuesMap'] = $valuesMap;
- }
- }
- return $attributes;
- }
- public function getXsdFieldType($fieldName) {// @returns string - xsd type, TODO: throw Exception when field not found
- if ($this->loadSchema()) {
- $schemaType = $this->_schemaClass->getType($fieldName);
- if ($schemaType) return $schemaType;
- }
- $fldType = 'xsd:string';
- if ('A_RECORD_UPDATE_DATE' == $fieldName) return 'xsd:string';
- if ('A_RECORD_CREATE_DATE' == $fieldName) return 'xsd:string';
- if ($this->isIntegerField($fieldName)) return 'xsd:integer';
- else if ($this->isDecimalField($fieldName)) return 'xsd:decimal';
- else if ($this->isDateTimeField($fieldName)) return 'xsd:dateTime';
- else if ($this->isDateField($fieldName)) return 'xsd:date';
- else if ($this->isGeomField($fieldName)) {
- //$fldType = 'gml:GeometryPropertyType';
- $geomType = $this->getGeomFieldType($fieldName);
- if ('polygon' == $geomType) return 'gml:PolygonPropertyType';
- else if ('point' == $geomType) return 'gml:PointPropertyType';
- else if ('linestring' == $geomType) return 'gml:LineStringPropertyType';
- else return 'gml:GeometryPropertyType';
- }
- else if ($this->isEnumerationField($fieldName)) return 'xsd:string';
- else if ($this->isBinaryField($fieldName)) return 'xsd:base64Binary';
- return $fldType;
- }
- public function getXsdFieldParam($fieldName, $paramKey) {
- return ($this->loadSchema())
- ? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
- : null
- ;
- }
- public function getXsdTypes() {
- $xsdTypes = [];
- foreach ($this->getFields() as $idZasob => $field) {
- $xsdTypes[ $field['name'] ] = [
- 'name' => $field['name'],
- 'perms' => $field['perms'],
- 'idZasob' => $idZasob,
- 'xsdType' => $this->getXsdFieldType($field['name']),
- ];
- }
- return $xsdTypes;
- }
- public function getSqlFieldName($childName) {
- return $childName;
- }
- public function hasField($fieldName) {// TODO: fix use Core_AclFields or ACL::getObjectFields($namespace) - use cache - one place to store structure
- return array_key_exists($fieldName, $this->_types);
- }
- }
|