1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: class RecordSelector {
48: 49: 50: 51: 52: 53:
54: private $relationObject = null;
55:
56: 57: 58: 59: 60: 61:
62: private $label;
63:
64: 65: 66: 67: 68: 69: 70:
71: private $accessingClassName;
72:
73: 74: 75: 76: 77: 78:
79: private $onloadJS = '';
80:
81: 82: 83: 84: 85: 86:
87: private $name;
88:
89: 90: 91: 92: 93: 94:
95: private static $logger = null;
96:
97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
107: public function __construct($relation, $label='', $name='', $accessingClassName='') {
108: self::$logger = new Logger('RecordSelector');
109: self::$logger->debug('>>__construct(relation=['.$relation.'], label=['.$label.'], name=['.$name.'], accessingClassName=['.$accessingClassName.'])');
110:
111: if(!$relation instanceof Relation)
112: throw new IllegalArguementException('Invalid Relation object provided to the RecordSelector constructor!');
113:
114: $this->relationObject = $relation;
115: $this->label = $label;
116: $this->name = $name;
117: $this->accessingClassName = $accessingClassName;
118:
119: self::$logger->debug('<<__construct');
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: public function render($expanded=false, $buttons=true) {
131: self::$logger->debug('>>render(expanded=['.$expanded.'], buttons=['.$buttons.'])');
132:
133: global $config;
134:
135: $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt($this->name)) : $this->name);
136:
137: $html = '';
138:
139:
140: if($this->relationObject->getRelationType() == 'MANY-TO-ONE') {
141:
142: $inputBoxValue = $this->relationObject->getRelatedClassDisplayFieldValue();
143:
144: $html .= '<div class="form-group">';
145: $html .= '<label for="'.$this->name.'_display">'.$this->label.'</label>';
146:
147: $html .= '<input type="text" size="70" class="form-control" name="'.$this->name.'_display" id="'.$this->name.'_display" value="'.$inputBoxValue.'" disabled/>';
148:
149: $js = " if(window.jQuery) {
150: window.jQuery.dialog = new BootstrapDialog({
151: title: 'Please select',
152: message: 'Loading...',
153: onshow: function(dialogRef){
154: dialogRef.getModalBody().load('".$config->get('app.url')."ViewRecordSelector/value/'+document.getElementById('".$fieldname."').value+'/field/".$this->name."/relatedClass/".$this->relationObject->getRelatedClass()."/relatedClassField/".$this->relationObject->getRelatedClassField()."/relatedClassDisplayField/".$this->relationObject->getRelatedClassDisplayField()."/relationType/".$this->relationObject->getRelationType()."');
155: },
156: buttons: [
157: {
158: icon: 'glyphicon glyphicon-remove',
159: label: 'Cancel',
160: cssClass: 'btn btn-default btn-xs',
161: action: function(dialogItself){
162: dialogItself.close();
163: }
164: }
165: ]
166: });
167: window.jQuery.dialog.open();
168: }";
169:
170:
171: $tmp = new Button($js, "Select", "relBut", '', 'glyphicon-check');
172: $html .= '<div class="centered lower">'.$tmp->render().'</div>';
173:
174:
175: $html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="'.$this->relationObject->getValue().'"/>';
176:
177: if($this->relationObject->getRule() != '') {
178: $html .= '<input type="hidden" id="'.$fieldname.'_msg" value="'.$this->relationObject->getHelper().'"/>';
179: $html .= '<input type="hidden" id="'.$fieldname.'_rule" value="'.$this->relationObject->getRule().'"/>';
180: }
181:
182: $html .= '</div>';
183: }
184:
185:
186: if($this->relationObject->getRelationType() == 'ONE-TO-MANY') {
187: $objects = $this->relationObject->getRelatedObjects();
188:
189: if(count($objects) > 0) {
190:
191: if($this->name == 'tags' && $this->relationObject->getRelatedClass() == 'TagObject') {
192: $html .= '<p><strong>'.$this->label.':</strong>';
193:
194: foreach($objects as $tag) {
195: $html .= ' <a href="'.$config->get('app.url').'search/q/'.$tag->get('content').'">'.$tag->get('content').'</a>';
196: }
197:
198: $html .= '</p>';
199: }else{
200: $html .= '<div><strong>'.$this->label.':</strong>';
201: if($buttons) {
202: $html .= '<div class="spread">';
203: $tmp = new Button("document.getElementById('relation_field_".$this->name."').style.display = '';", "Show", $this->name."DisBut", '', 'glyphicon-list');
204: $html .= $tmp->render();
205: $tmp = new Button("document.getElementById('relation_field_".$this->name."').style.display = 'none';", "Hide", $this->name."HidBut", '', 'glyphicon-minus');
206: $html .= $tmp->render();
207: $html .= '</div>';
208: }
209: $html .= '</div>';
210:
211: $html .= '<div id="relation_field_'.$this->name.'" style="display:'.($expanded ? '' : 'none').';">';
212:
213: $customViewControllerName = AlphaController::getCustomControllerName(get_class($objects[0]), 'view');
214: $customEditControllerName = AlphaController::getCustomControllerName(get_class($objects[0]), 'edit');
215:
216: foreach($objects as $obj) {
217:
218:
219: if(mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
220: $viewURL = FrontController::generateSecureURL('act=Detail&bo='.get_class($obj).'&oid='.$obj->getOID());
221: $editURL = FrontController::generateSecureURL('act=Edit&bo='.get_class($obj).'&oid='.$obj->getOID());
222: }else{
223: if(isset($customViewControllerName)) {
224: if($config->get('app.use.mod.rewrite'))
225: $viewURL = $config->get('app.url').$customViewControllerName.'/oid/'.$obj->getOID();
226: else
227: $viewURL = $config->get('app.url').'controller/'.$customViewControllerName.'.php?oid='.$obj->getOID();
228: }else{
229: $viewURL = $config->get('app.url').'alpha/controller/Detail.php?bo='.get_class($obj).'&oid='.$obj->getOID();
230: }
231: if(isset($customEditControllerName)) {
232: if($config->get('app.use.mod.rewrite'))
233: $editURL = $config->get('app.url').$customEditControllerName.'/oid/'.$obj->getOID();
234: else
235: $editURL = $config->get('app.url').'controller/'.$customEditControllerName.'.php?oid='.$obj->getOID();
236: }else{
237: $editURL = $config->get('app.url').'alpha/controller/Edit.php?bo='.get_class($obj).'&oid='.$obj->getOID();
238: }
239: }
240:
241: 242: 243: 244:
245: $headerFields = $this->relationObject->getRelatedClassHeaderFields();
246: if(count($headerFields) > 0) {
247: foreach($headerFields as $field) {
248: $label = $obj->getDataLabel($field);
249: $value = $obj->get($field);
250:
251: if($field == 'created_by' || $field == 'updated_by') {
252: $person = new PersonObject();
253: $person->load($value);
254: $value = $person->getDisplayName();
255: }
256:
257: $html .= '<em>'.$label.': </em>'.$value.' ';
258: }
259:
260: if($obj->getCreateTS() != $obj->getUpdateTS()) {
261: try {
262: $html .= '<em>'.$obj->getDataLabel('updated_ts').': </em>'.$obj->get('updated_ts');
263: }catch(IllegalArguementException $e) {
264: $html .= '<em>Updated: </em>'.$obj->get('updated_ts');
265: }
266: }
267: }else{
268: $html .= '<em>'.$obj->getDataLabel('OID').': </em>'.$obj->get('OID');
269: }
270:
271: $value = str_replace("\n", '<br>', $obj->get($this->relationObject->getRelatedClassDisplayField()));
272: $html .= '<p>'.$value.'</p>';
273:
274: $html .= '<div class="centered">';
275: $html .= '<a href="'.$viewURL.'">View</a>';
276:
277: if(isset($_SESSION['currentUser']) && $_SESSION['currentUser']->getOID() == $obj->getCreatorId())
278: $html .= ' <a href="'.$editURL.'">Edit</a>';
279: $html .= '</div>';
280: }
281: $html .= '</div>';
282: }
283: }
284: }
285:
286:
287: if($this->relationObject->getRelationType() == 'MANY-TO-MANY') {
288:
289: $inputBoxValue = $this->relationObject->getRelatedClassDisplayFieldValue($this->accessingClassName);
290:
291: $inputBoxValue = str_replace(",", "\n", $inputBoxValue);
292:
293: $html .= '<div class="form-group">';
294: $html .= '<label for="'.$this->name.'_display">'.$this->label.'</label>';
295:
296: $html .= '<textarea id="'.$this->name.'_display" class="form-control" rows="5" readonly>';
297: $html .= $inputBoxValue;
298: $html .= '</textarea>';
299:
300: $fieldname1 = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt($this->name)) : $this->name);
301: $fieldname2 = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt($this->name.'_OID')) : $this->name.'_OID');
302:
303:
304: $js = "if(window.jQuery) {
305: BootstrapDialog.show({
306: title: 'Please select',
307: message: 'Loading...',
308: onshow: function(dialogRef){
309: dialogRef.getModalBody().load('".$config->get('app.url')."ViewRecordSelector/lookupOIDs/'+document.getElementById('".$fieldname1."').value+'/value/'+document.getElementById('".$fieldname2."').value+'/field/".$this->name."/relatedClassLeft/".$this->relationObject->getRelatedClass('left')."/relatedClassLeftDisplayField/".$this->relationObject->getRelatedClassDisplayField('left')."/relatedClassRight/".$this->relationObject->getRelatedClass('right')."/relatedClassRightDisplayField/".$this->relationObject->getRelatedClassDisplayField('right')."/accessingClassName/".$this->accessingClassName."/relationType/".$this->relationObject->getRelationType()."');
310: },
311: buttons: [
312: {
313: icon: 'glyphicon glyphicon-remove',
314: label: 'Cancel',
315: cssClass: 'btn btn-default btn-xs',
316: action: function(dialogItself){
317: dialogItself.close();
318: }
319: },
320: {
321: icon: 'glyphicon glyphicon-ok',
322: label: 'Okay',
323: cssClass: 'btn btn-default btn-xs',
324: action: function(dialogItself) {
325: setParentFieldValues();
326: $('[id=\'".$this->name."_display\']').blur();
327: dialogItself.close();
328: }
329: }
330: ]
331: });
332: }";
333:
334: $tmp = new Button($js, "Select", "relBut", '', 'glyphicon-check');
335: $html .= '<div class="centered lower">'.$tmp->render().'</div>';
336:
337: $html .= '</div>';
338:
339:
340: $html .= '<input type="hidden" name="'.$fieldname2.'" id="'.$fieldname2.'" value="'.$this->relationObject->getValue().'"/>';
341:
342:
343: if($this->relationObject->getSide($this->accessingClassName) == 'left')
344: $lookupOIDs = $this->relationObject->getLookup()->loadAllFieldValuesByAttribute('leftID', $this->relationObject->getValue(), 'rightID', 'DESC');
345: else
346: $lookupOIDs = $this->relationObject->getLookup()->loadAllFieldValuesByAttribute('rightID', $this->relationObject->getValue(), 'leftID', 'DESC');
347:
348: $html .= '<input type="hidden" name="'.$fieldname1.'" id="'.$fieldname1.'" value="'.implode(',', $lookupOIDs).'"/>';
349: }
350:
351: self::$logger->debug('<<__render [html]');
352: return $html;
353: }
354:
355: 356: 357: 358: 359: 360: 361:
362: public function renderSelector($lookupOIDs=array()) {
363: self::$logger->debug('>>renderSelector(lookupOIDs=['.var_export($lookupOIDs, true).'])');
364:
365: global $config;
366:
367: $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt($_GET['field'])) : $_GET['field']);
368:
369:
370: $html = '<script language="JavaScript">
371: var selectedOIDs = new Object();
372:
373: function toggelOID(oid, displayValue, isSelected) {
374: if(isSelected)
375: selectedOIDs[oid] = displayValue;
376: else
377: delete selectedOIDs[oid];
378: }
379:
380: function setParentFieldValues() {
381: var OIDs;
382: var displayValues;
383:
384: for(key in selectedOIDs) {
385: if(OIDs == null)
386: OIDs = key;
387: else
388: OIDs = OIDs + \',\' + key;
389:
390: if(displayValues == null)
391: displayValues = selectedOIDs[key];
392: else
393: displayValues = displayValues + \'\\n\' + selectedOIDs[key];
394: }
395:
396: if(OIDs == null) {
397: document.getElementById(\''.$fieldname.'\').value = "00000000000";
398: document.getElementById(\''.$_GET['field'].'_display\').value = "";
399: }else{
400: document.getElementById(\''.$fieldname.'\').value = OIDs;
401: document.getElementById(\''.$_GET['field'].'_display\').value = displayValues;
402: }
403: }
404:
405: </script>';
406:
407:
408: if($this->relationObject->getRelationType() == 'MANY-TO-MANY') {
409:
410: $classNameLeft = $this->relationObject->getRelatedClass('left');
411: $classNameRight = $this->relationObject->getRelatedClass('right');
412:
413: if($this->accessingClassName == $classNameLeft) {
414: AlphaDAO::loadClassDef($classNameRight);
415: $tmpObject = new $classNameRight;
416: $fieldName = $this->relationObject->getRelatedClassDisplayField('right');
417: $fieldLabel = $tmpObject->getDataLabel($fieldName);
418: $oidLabel = $tmpObject->getDataLabel('OID');
419:
420: $objects = $tmpObject->loadAll(0, 0, 'OID', 'ASC', true);
421:
422: self::$logger->debug('['.count($objects).'] related ['.$classNameLeft.'] objects loaded');
423: }else{
424: AlphaDAO::loadClassDef($classNameLeft);
425: $tmpObject = new $classNameLeft;
426: $fieldName = $this->relationObject->getRelatedClassDisplayField('left');
427: $fieldLabel = $tmpObject->getDataLabel($fieldName);
428: $oidLabel = $tmpObject->getDataLabel('OID');
429:
430: $objects = $tmpObject->loadAll(0, 0, 'OID', 'ASC', true);
431:
432: self::$logger->debug('['.count($objects).'] related ['.$classNameLeft.'] objects loaded');
433: }
434:
435: $html .= '<table cols="3" class="table table-bordered">';
436: $html .= '<tr>';
437: $html .= '<th>'.$oidLabel.'</th>';
438: $html .= '<th>'.$fieldLabel.'</th>';
439: $html .= '<th>Connect?</th>';
440: $html .= '</tr>';
441:
442: foreach($objects as $obj){
443: $html .= '<tr>';
444: $html .= '<td width="20%">';
445: $html .= $obj->getOID();
446: $html .= '</td>';
447: $html .= '<td width="60%">';
448: $html .= $obj->get($fieldName);
449: $html .= '</td>';
450: $html .= '<td width="20%">';
451:
452: if(in_array($obj->getOID(), $lookupOIDs)) {
453: $this->onloadJS .= 'toggelOID(\''.$obj->getOID().'\',\''.$obj->get($fieldName).'\',true);';
454: $html .= '<input name = "'.$obj->getOID().'" type="checkbox" checked onclick="toggelOID(\''.$obj->getOID().'\',\''.$obj->get($fieldName).'\',this.checked);"/>';
455: }else{
456: $html .= '<input name = "'.$obj->getOID().'" type="checkbox" onclick="toggelOID(\''.$obj->getOID().'\',\''.$obj->get($fieldName).'\',this.checked);"/>';
457: }
458: $html .= '</td>';
459: $html .= '</tr>';
460: }
461: $html .= '</table>';
462:
463: }else{
464: $className = $this->relationObject->getRelatedClass();
465:
466: AlphaDAO::loadClassDef($className);
467:
468: $tmpObject = new $className;
469: $label = $tmpObject->getDataLabel($this->relationObject->getRelatedClassDisplayField());
470: $oidLabel = $tmpObject->getDataLabel('OID');
471:
472: $objects = $tmpObject->loadAll(0, 0, 'OID', 'DESC');
473:
474: $html = '<table cols="3" width="100%" class="bordered">';
475: $html .= '<tr>';
476: $html .= '<th>'.$oidLabel.'</th>';
477: $html .= '<th>'.$label.'</th>';
478: $html .= '<th>Connect?</th>';
479: $html .= '</tr>';
480:
481: foreach($objects as $obj){
482: $html .= '<tr>';
483: $html .= '<td width="20%">';
484: $html .= $obj->getOID();
485: $html .= '</td>';
486: $html .= '<td width="60%">';
487: $html .= $obj->get($this->relationObject->getRelatedClassDisplayField());
488: $html .= '</td>';
489: $html .= '<td width="20%">';
490: if($obj->getOID() == $this->relationObject->getValue()) {
491: $html .= '<img src="'.$config->get('app.url').'/alpha/images/icons/accept_ghost.png"/>';
492: }else{
493: $tmp = new Button("document.getElementById('".$fieldname."').value = '".$obj->getOID()."'; document.getElementById('".$_GET['field']."_display').value = '".$obj->get($this->relationObject->getRelatedClassDisplayField())."'; $('[Id=".$_GET['field']."_display]').blur(); window.jQuery.dialog.close();", "", "selBut", $config->get('app.url')."/alpha/images/icons/accept.png");
494: $html .= $tmp->render();
495: }
496: $html .= '</td>';
497: $html .= '</tr>';
498: }
499: $html .= '</table>';
500: }
501:
502: $html .= '<script type="text/javascript">'.
503: '$(document).ready(function() {';
504:
505: $html .= $this->onloadJS;
506:
507: $html .= '});</script>';
508:
509: self::$logger->debug('<<renderSelector[html]');
510: return $html;
511: }
512: }
513:
514: ?>