| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817 |
- <?php
- Lib::loadClass('Core_AclBase');
- Lib::loadClass('SchemaException');
- /**
- * $_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 {
- private $_zasobID = '';
- private $_db = '';
- private $_name = '';
- private $_label = '';
- private $_opis = '';
- private $_fields = array();
- private $_types = array();
- private $_virtualFieldsIdList = array();
- private $_schemaLoaded = false;
- public function __construct($zasobID) { $this->_zasobID = $zasobID; }
- public function getSourceName() { return 'default_db'; }
- public function getName() { return $this->_name; }
- public function getRootTableName() { return $this->_name; }
- public function getID() { return $this->_zasobID; }
- public function 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->_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) {// TODO: legacy
- 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 hasEditPerms() {
- foreach ($this->_fields 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 hasCreatePerms() {
- foreach ($this->_fields as $kFldID => $vFld) {
- if ($this->hasFieldPerm($kFldID, 'C')) return true;
- }
- return false;
- }
- 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) {// 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->_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
- ,'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')) {
- if ($this->hasFieldPerm($idZasob, 'V')) return true;
- }
- return true;
- }
- public function canReadObjectField($fieldName, $record) {
- if (!$this->canReadField($fieldName)) return false;
- if (!$record) return false;
- $idZasob = $this->getFieldIdByName($fieldName);
- if (!$idZasob) 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->_fields[$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>';
- }
- }
- Lib::loadClass('Typespecial');
- $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->_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}";
- }
- */
- }
- 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__);
- Lib::loadClass('ProcesHelper');
- $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->_fields
- $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->save();
- $_cache[$idTable] = $obj;
- return $_cache[$idTable];
- }
- public function loadSchema() {
- if ($this->_schemaLoaded) return;
- $srvName = $_SERVER['SERVER_NAME'];
- Lib::loadClass('Schema_TableFactory');
- $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
- $this->_schemaLoaded = true;
- }
- public function init($force = false) {
- $this->loadSchema();
- if (empty($this->_fields)) {
- $this->_types = array();// clear _types @see $this->isInitialized
- $userAcl = User::getAcl();
- $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
- DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__ );
- $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;
- }
- $ds = $this->getDataSource();
- $this->_types = $ds->getFieldTypes();
- uasort($this->_fields, array($this, 'sortFieldsCallback'));
- $this->_fixTypes();
- $this->save();
- }
- public function _fixTypes() {
- $this->_fixDateFields();
- $this->_sortEnumFields();
- $this->_fixProjectType();
- if ($this->_schemaClass) $this->_types = $this->_schemaClass->fixTypes($this->_types);
- //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
- $fieldIds = array_keys($this->_fields);
- Lib::loadClass('Typespecial');
- $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->hasField($vFieldConfig->ID_CELL).') ';echo'</pre>';
- if (!$this->hasField($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->hasField($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();
- }
- 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 getRealFieldListByIdZasob() {
- $cols = array();
- foreach ($this->_fields as $idField => $fld) {
- if ($fld['name'] == 'ID') {
- $cols[$idField] = 'ID';
- }
- }
- foreach ($this->_fields as $idField => $fld) {
- if ($fld['name'] == 'ID') continue;
- if (array_key_exists($fld['name'], $this->_types)) {
- $cols[$idField] = $fld['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 = 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['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') {
- $item = array();
- $fields = $this->getFields();
- foreach ($fields as $kID => $vField) {
- $vFieldName = $vField['name'];
- if (array_key_exists("f{$kID}", $args)) {
- $value = $args["f{$kID}"];
- if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
- $value = $this->fixEmptyValueFromUser($kID);
- }
- $item[$vFieldName] = $value;
- }
- }
- return $item;
- }
- public function getItem($id) {
- $ds = $this->getDataSource();
- return $ds->getItem($id);
- }
- public function getItems($params = array()) {
- $ds = $this->getDataSource();
- return $ds->getItems($params);
- }
- public function getTotal($params) {
- $ds = $this->getDataSource();
- return $ds->getTotal($params);
- }
- 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($fldName) {
- $dbGeomType = $this->getFieldType($fldName);
- $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
- $geomType = strtolower($dbGeomType);
- return $geomType;
- }
- public function getHistItems($id) {
- $ds = $this->getDataSource();
- return $ds->getHistItems($id);
- }
- 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);
- }
- if (empty($itemTodo)) {
- //throw new Exception('Item patch is empty');
- DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
- return 0;// nothing to insert
- }
- $ds = $this->getDataSource();
- // from convertObjectFromUserInput
- $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;
- }
- }
- if (empty($item)) {
- throw new Exception("Nothing to add");
- }
- {// 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
- }
- $ds = $this->getDataSource();
- $primaryKeyField = $ds->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;
- $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 = new stdClass();
- foreach ($types as $kName => $vType) {
- if ($kName == $primaryKeyField) {
- continue;
- } else 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 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'));
- $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) {
- Lib::loadClass('DataSourceFactory');
- $dsConfig = array();
- $dsConfig['source_id'] = $this->getDB();
- $dsConfig['object_name'] = $this->getName();
- $dsConfig['fields'] = $cols;
- $dsConfig['field_types'] = $this->getTypes();
- $dsConfig['fields_virtual'] = $this->getVirtualFieldList();
- $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
- return DataSourceFactory::buildFromZasobInfo($dsConfig);
- }
- public function getPrimaryKeyField() {
- $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($fldName) {// @returns string - xsd type, TODO: throw Exception when field not found
- $fldType = 'xsd:string';
- if ($this->isIntegerField($fldName)) return 'xsd:integer';
- else if ($this->isDecimalField($fldName)) return 'xsd:decimal';
- else if ($this->isDateField($fldName)) return 'xsd:date';
- else if ($this->isDateTimeField($fldName)) return 'xsd:dateTime';
- else if ($this->isGeomField($fldName)) {
- //$fldType = 'gml:GeometryPropertyType';
- $geomType = $this->getGeomFieldType($fldName);
- 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($fldName)) return 'xsd:string';
- else if ($this->isBinaryField($fldName)) return 'xsd:base64Binary';
- return $fldType;
- }
- }
|