![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 <?php /* $Id: CustomFields.class.php 135 2008-04-04 13:49:13Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/classes/CustomFields.class.php $ */ 2 if (!defined('W2P_BASE_DIR')) { 3 die('You should not access this file directly.'); 4 } 5 /* 6 * CustomField Classes 7 */ 8 9 class CustomField { 10 var $field_id; 11 // TODO - Implement Field Order - some people like to change the order of fields 12 var $field_order; 13 var $field_name; 14 var $field_description; 15 var $field_htmltype; 16 var $field_published; 17 // TODO - data type, meant for validation if you just want numeric data in a text input 18 // but not yet implemented 19 var $field_datatype; 20 21 var $field_extratags; 22 23 var $object_id = null; 24 25 var $value_id = 0; 26 27 var $value_charvalue; 28 var $value_intvalue; 29 30 function CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 31 $this->field_id = $field_id; 32 $this->field_name = $field_name; 33 $this->field_order = $field_order; 34 $this->field_description = $field_description; 35 $this->field_extratags = $field_extratags; 36 $this->field_published = $field_published; 37 } 38 39 function load($object_id) { 40 // Override Load Method for List type Classes 41 global $db; 42 $q = new DBQuery; 43 $q->addTable('custom_fields_values'); 44 $q->addWhere('value_field_id = ' . $this->field_id); 45 $q->addWhere('value_object_id = ' . $object_id); 46 $rs = $q->exec(); 47 $row = $q->fetchRow(); 48 $q->clear(); 49 50 $value_id = $row['value_id']; 51 $value_charvalue = $row['value_charvalue']; 52 $value_intvalue = $row['value_intvalue']; 53 54 if ($value_id != null) { 55 $this->value_id = $value_id; 56 $this->value_charvalue = $value_charvalue; 57 $this->value_intvalue = $value_intvalue; 58 } 59 } 60 61 function store($object_id) { 62 global $db; 63 if ($object_id == null) { 64 return 'Error: Cannot store field (' . $this->field_name . '), associated id not supplied.'; 65 } else { 66 $ins_intvalue = $this->value_intvalue == null ? '0' : $this->value_intvalue; 67 $ins_charvalue = $this->value_charvalue == null ? '' : stripslashes($this->value_charvalue); 68 69 if ($this->value_id > 0) { 70 $q = new DBQuery; 71 $q->addTable('custom_fields_values'); 72 $q->addUpdate('value_charvalue', $ins_charvalue); 73 $q->addUpdate('value_intvalue', $ins_intvalue); 74 $q->addWhere('value_id = ' . $this->value_id); 75 } else { 76 $q = new DBQuery; 77 $q->addTable('custom_fields_values'); 78 $q->addQuery('MAX(value_id)'); 79 $max_id = $q->loadResult(); 80 $new_value_id = $max_id ? $max_id + 1 : 1; 81 82 $q = new DBQuery; 83 $q->addTable('custom_fields_values'); 84 $q->addInsert('value_id', $new_value_id); 85 $q->addInsert('value_module', ''); 86 $q->addInsert('value_field_id', $this->field_id); 87 $q->addInsert('value_object_id', $object_id); 88 89 $q->addInsert('value_charvalue', $ins_charvalue); 90 $q->addInsert('value_intvalue', $ins_intvalue); 91 } 92 $rs = $q->exec(); 93 94 $q->clear(); 95 if (!$rs) { 96 return $db->ErrorMsg() . ' | SQL: '; 97 } 98 } 99 } 100 101 function setIntValue($v) { 102 $this->value_intvalue = $v; 103 } 104 105 function intValue() { 106 return $this->value_intvalue; 107 } 108 109 function setValue($v) { 110 $this->value_charvalue = $v; 111 } 112 113 function value() { 114 return $this->value_charvalue; 115 } 116 117 function charValue() { 118 return $this->value_charvalue; 119 } 120 121 function setValueId($v) { 122 $this->value_id = $v; 123 } 124 125 function valueId() { 126 return $this->value_id; 127 } 128 129 function fieldName() { 130 return $this->field_name; 131 } 132 133 function fieldDescription() { 134 return $this->field_description; 135 } 136 137 function fieldId() { 138 return $this->field_id; 139 } 140 141 function fieldHtmlType() { 142 return $this->field_htmltype; 143 } 144 145 function fieldExtraTags() { 146 return $this->field_extratags; 147 } 148 149 function fieldOrder() { 150 return $this->field_order; 151 } 152 153 function fieldPublished() { 154 return $this->field_published; 155 } 156 157 } 158 159 // CustomFieldCheckBox - Produces an INPUT Element of the CheckBox type in edit mode, view mode indicates 'Yes' or 'No' 160 class CustomFieldCheckBox extends CustomField { 161 function CustomFieldCheckBox($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 162 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 163 $this->field_htmltype = 'checkbox'; 164 } 165 166 function getHTML($mode) { 167 switch ($mode) { 168 case 'edit': 169 $bool_tag = ($this->intValue()) ? 'checked="checked"': 170 ''; 171 $html = $this->field_description . ': </td><td><input type="checkbox" name="' . $this->field_name . '" value="1" ' . $bool_tag . $this->field_extratags . '/>'; 172 break; 173 case 'view': 174 $bool_text = ($this->intValue()) ? 'Yes': 175 'No'; 176 $html = $this->field_description . ': </td><td class="hilite" width="100%">' . $bool_text; 177 break; 178 } 179 return $html; 180 } 181 182 function setValue($v) { 183 $this->value_intvalue = $v; 184 } 185 } 186 187 // CustomFieldText - Produces an INPUT Element of the TEXT type in edit mode 188 class CustomFieldText extends CustomField { 189 function CustomFieldText($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 190 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 191 $this->field_htmltype = 'textinput'; 192 } 193 194 function getHTML($mode) { 195 switch ($mode) { 196 case 'edit': 197 $html = $this->field_description . ': </td><td><input type="text" class="text" name="' . $this->field_name . '" value="' . $this->charValue() . '" ' . $this->field_extratags . ' />'; 198 break; 199 case 'view': 200 $html = $this->field_description . ': </td><td class="hilite" width="100%">' . $this->charValue(); 201 break; 202 } 203 return $html; 204 } 205 } 206 207 // CustomFieldTextArea - Produces a TEXTAREA Element in edit mode 208 class CustomFieldTextArea extends CustomField { 209 function CustomFieldTextArea($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 210 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 211 $this->field_htmltype = 'textarea'; 212 } 213 214 function getHTML($mode) { 215 switch ($mode) { 216 case 'edit': 217 $html = $this->field_description . ': </td><td><textarea name="' . $this->field_name . '" ' . $this->field_extratags . '>' . $this->charValue() . '</textarea>'; 218 break; 219 case 'view': 220 $html = $this->field_description . ': </td><td class="hilite" width="100%">' . nl2br($this->charValue()); 221 break; 222 } 223 return $html; 224 } 225 } 226 227 // CustomFieldLabel - Produces just a non editable label 228 class CustomFieldLabel extends CustomField { 229 function CustomFieldLabel($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 230 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 231 $this->field_htmltype = 'label'; 232 } 233 234 function getHTML($mode) { 235 // We don't really care about its mode 236 return '<span ' . $this->field_extratags . '>' . $this->field_description . '</span>'; 237 } 238 } 239 240 // CustomFieldSeparator - Produces just an horizontal line 241 class CustomFieldSeparator extends CustomField { 242 function CustomFieldSeparator($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 243 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 244 $this->field_htmltype = 'separator'; 245 } 246 247 function getHTML($mode) { 248 // We don't really care about its mode 249 return '<hr ' . $this->field_extratags . ' />'; 250 } 251 } 252 253 // CustomFieldSelect - Produces a SELECT list, extends the load method so that the option list can be loaded from a seperate table 254 class CustomFieldSelect extends CustomField { 255 var $options; 256 257 function CustomFieldSelect($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 258 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 259 $this->field_htmltype = 'select'; 260 $this->options = new CustomOptionList($field_id); 261 $this->options->load(); 262 } 263 264 function getHTML($mode) { 265 switch ($mode) { 266 case 'edit': 267 $html = $this->field_description . ': </td><td>'; 268 $html .= $this->options->getHTML($this->field_name, $this->intValue()); 269 break; 270 case 'view': 271 $html = $this->field_description . ': </td><td class="hilite" width="100%">' . $this->options->itemAtIndex($this->intValue()); 272 break; 273 } 274 return $html; 275 } 276 277 function setValue($v) { 278 $this->value_intvalue = $v; 279 } 280 281 function value() { 282 return $this->value_intvalue; 283 } 284 } 285 286 /* CustomFieldWeblink 287 ** Produces an INPUT Element of the TEXT type in edit mode 288 ** and a <a href> </a> weblink in display mode 289 */ 290 291 class CustomFieldWeblink extends CustomField { 292 function CustomFieldWeblink($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published) { 293 $this->CustomField($field_id, $field_name, $field_order, $field_description, $field_extratags, $field_published); 294 $this->field_htmltype = 'href'; 295 } 296 297 function getHTML($mode) { 298 switch ($mode) { 299 case 'edit': 300 $html = $this->field_description . ': </td><td><input type="text" class="text" name="' . $this->field_name . '" value="' . $this->charValue() . '" ' . $this->field_extratags . ' />'; 301 break; 302 case 'view': 303 $html = $this->field_description . ': </td><td class="hilite" width="100%"><a href="' . $this->charValue() . '">' . $this->charValue() . '</a>'; 304 break; 305 } 306 return $html; 307 } 308 } 309 310 // CustomFields class - loads all custom fields related to a module, produces a html table of all custom fields 311 // Also loads values automatically if the obj_id parameter is supplied. The obj_id parameter is the ID of the module object 312 // eg. company_id for companies module 313 class CustomFields { 314 var $m; 315 var $a; 316 var $mode; 317 var $obj_id; 318 var $order; 319 var $published; 320 321 var $fields; 322 323 function CustomFields($m, $a, $obj_id = null, $mode = 'edit', $published = 0) { 324 $this->m = $m; 325 $this->a = 'addedit'; // only addedit pages can carry the custom field for now 326 $this->obj_id = $obj_id; 327 $this->mode = $mode; 328 $this->published = $published; 329 330 // Get Custom Fields for this Module 331 $q = new DBQuery; 332 $q->addTable('custom_fields_struct'); 333 $q->addWhere('field_module = \'' . $this->m . '\' AND field_page = \'' . $this->a . '\''); 334 if ($published) { 335 $q->addWhere('field_published = 1'); 336 } 337 $q->addOrder('field_order ASC'); 338 $rows = $q->loadList(); 339 if ($rows == null) { 340 // No Custom Fields Available 341 } else { 342 foreach ($rows as $row) { 343 switch ($row['field_htmltype']) { 344 case 'checkbox': 345 $this->fields[$row['field_name']] = new CustomFieldCheckbox($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 346 break; 347 case 'href': 348 $this->fields[$row['field_name']] = new CustomFieldWeblink($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 349 break; 350 case 'textarea': 351 $this->fields[$row['field_name']] = new CustomFieldTextArea($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 352 break; 353 case 'select': 354 $this->fields[$row['field_name']] = new CustomFieldSelect($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 355 break; 356 case 'label': 357 $this->fields[$row['field_name']] = new CustomFieldLabel($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 358 break; 359 case 'separator': 360 $this->fields[$row['field_name']] = new CustomFieldSeparator($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 361 break; 362 default: 363 $this->fields[$row['field_name']] = new CustomFieldText($row['field_id'], $row['field_name'], $row['field_order'], stripslashes($row['field_description']), stripslashes($row['field_extratags']), $row['field_order'], $row['field_published']); 364 break; 365 } 366 } 367 368 if ($obj_id > 0) { 369 //Load Values 370 foreach ($this->fields as $key => $cfield) { 371 $this->fields[$key]->load($this->obj_id); 372 } 373 } 374 } 375 376 } 377 378 function add($field_name, $field_description, $field_htmltype, $field_datatype, $field_extratags, $field_order, $field_published, &$error_msg) { 379 global $db; 380 381 $q = new DBQuery; 382 $q->addTable('custom_fields_struct'); 383 $q->addQuery('MAX(field_id)'); 384 $max_id = $q->loadResult(); 385 $next_id = $max_id ? $max_id + 1 : 1; 386 387 $field_order = $field_order ? $field_order : 1; 388 $field_published = $field_published ? 1 : 0; 389 390 $field_a = 'addedit'; 391 392 // TODO - module pages other than addedit 393 // TODO - validation that field_name doesnt already exist 394 $q = new DBQuery; 395 $q->addTable('custom_fields_struct'); 396 $q->addInsert('field_id', $next_id); 397 $q->addInsert('field_module', $this->m); 398 $q->addInsert('field_page', $field_a); 399 $q->addInsert('field_htmltype', $field_htmltype); 400 $q->addInsert('field_datatype', $field_datatype); 401 $q->addInsert('field_order', $field_order); 402 $q->addInsert('field_name', $field_name); 403 $q->addInsert('field_description', $field_description); 404 $q->addInsert('field_extratags', $field_extratags); 405 $q->addInsert('field_order', $field_order); 406 $q->addInsert('field_published', $field_published); 407 408 if (!$q->exec()) { 409 $error_msg = $db->ErrorMsg(); 410 $q->clear(); 411 return 0; 412 } else { 413 $q->clear(); 414 return $next_id; 415 } 416 } 417 418 function update($field_id, $field_name, $field_description, $field_htmltype, $field_datatype, $field_extratags, $field_order, $field_published, &$error_msg) { 419 global $db; 420 421 $q = new DBQuery; 422 $q->addTable('custom_fields_struct'); 423 $q->addUpdate('field_name', $field_name); 424 $q->addUpdate('field_description', $field_description); 425 $q->addUpdate('field_htmltype', $field_htmltype); 426 $q->addUpdate('field_datatype', $field_datatype); 427 $q->addUpdate('field_extratags', $field_extratags); 428 $q->addUpdate('field_order', $field_order); 429 $q->addUpdate('field_published', $field_published); 430 $q->addWhere('field_id = ' . $field_id); 431 if (!$q->exec()) { 432 $error_msg = $db->ErrorMsg(); 433 $q->clear(); 434 return 0; 435 } else { 436 $q->clear(); 437 return $field_id; 438 } 439 } 440 441 function fieldWithId($field_id) { 442 foreach ($this->fields as $k => $v) { 443 if ($this->fields[$k]->field_id == $field_id) { 444 return $this->fields[$k]; 445 } 446 } 447 } 448 449 function bind(&$formvars) { 450 if (!count($this->fields) == 0) { 451 foreach ($this->fields as $k => $v) { 452 // if ($formvars[$k] != NULL) 453 // { 454 $this->fields[$k]->setValue(@$formvars[$k]); 455 // } 456 } 457 } 458 } 459 460 function store($object_id) { 461 if (!count($this->fields) == 0) { 462 $store_errors = ''; 463 foreach ($this->fields as $k => $cf) { 464 $result = $this->fields[$k]->store($object_id); 465 if ($result) { 466 $store_errors .= 'Error storing custom field ' . $k . ':' . $result; 467 } 468 } 469 470 //if ($store_errors) return $store_errors; 471 if ($store_errors) { 472 echo $store_errors; 473 } 474 } 475 } 476 477 function deleteField($field_id) { 478 global $db; 479 $q = new DBQuery; 480 $q->setDelete('custom_fields_struct'); 481 $q->addWhere('field_id = ' . $field_id); 482 if (!$q->exec()) { 483 $q->clear(); 484 return $db->ErrorMsg(); 485 } 486 } 487 488 function count() { 489 return count($this->fields); 490 } 491 492 function getHTML() { 493 if ($this->count() == 0) { 494 return ''; 495 } else { 496 $html = ''; 497 if (!$this->published) { 498 $html = '<table width="100%">'; 499 } 500 501 foreach ($this->fields as $cfield) { 502 if (!$this->published) { 503 $html .= "\t" . '<tr><td nowrap="nowrap">' . $cfield->getHTML($this->mode) . '</td></tr>'; 504 } else { 505 $html .= "\t" . '<tr><td align="right" nowrap="nowrap">' . $cfield->getHTML($this->mode) . '</td></tr>'; 506 } 507 } 508 if (!$this->published) { 509 $html .= '</table>'; 510 } 511 return $html; 512 } 513 } 514 515 function printHTML() { 516 $html = $this->getHTML(); 517 echo $html; 518 } 519 520 function search($moduleTable, $moduleTableId, $moduleTableName, $keyword) { 521 $q = new DBQuery; 522 $q->addTable('custom_fields_values', 'cfv'); 523 $q->addQuery('m.' . $moduleTableId); 524 $q->addQuery('m.' . $moduleTableName); 525 $q->addQuery('cfv.value_charvalue'); 526 $q->addJoin('custom_fields_struct', 'cfs', 'cfs.field_id = cfv.value_field_id'); 527 $q->addJoin($moduleTable, 'm', 'm.' . $moduleTableId . ' = cfv. value_object_id'); 528 $q->addWhere('cfs.field_module = \'' . $this->m . '\''); 529 $q->addWhere('cfv.value_charvalue LIKE \'%' . $keyword . '%\''); 530 return $q->loadList(); 531 } 532 } 533 534 class CustomOptionList { 535 var $field_id; 536 var $options; 537 538 function CustomOptionList($field_id) { 539 $this->field_id = $field_id; 540 $this->options = array(); 541 } 542 543 function load() { 544 global $db; 545 546 $q = new DBQuery; 547 $q->addTable('custom_fields_lists'); 548 $q->addWhere('field_id = ' . $this->field_id); 549 $q->addOrder('list_value'); 550 if (!$rs = $q->exec()) { 551 $q->clear(); 552 return $db->ErrorMsg(); 553 } 554 555 $this->options = array(); 556 557 while ($opt_row = $q->fetchRow()) { 558 $this->options[$opt_row['list_option_id']] = $opt_row['list_value']; 559 } 560 $q->clear(); 561 } 562 563 function store() { 564 global $db; 565 566 if (!is_array($this->options)) { 567 $this->options = array(); 568 } 569 570 //load the dbs options and compare them with the options 571 $q = new DBQuery; 572 $q->addTable('custom_fields_lists'); 573 $q->addWhere('field_id = ' . $this->field_id); 574 $q->addOrder('list_value'); 575 if (!$rs = $q->exec()) { 576 $q->clear(); 577 return $db->ErrorMsg(); 578 } 579 580 $dboptions = array(); 581 582 while ($opt_row = $q->fetchRow()) { 583 $dboptions[$opt_row['list_option_id']] = $opt_row['list_value']; 584 } 585 $q->clear(); 586 587 $newoptions = array(); 588 $newoptions = array_diff($this->options, $dboptions); 589 $deleteoptions = array_diff($dboptions, $this->options); 590 //insert the new options 591 foreach ($newoptions as $opt) { 592 $q = new DBQuery; 593 $q->addTable('custom_fields_lists'); 594 $q->addQuery('MAX(list_option_id)'); 595 $max_id = $q->loadResult(); 596 $optid = $max_id ? $max_id + 1 : 1; 597 598 $q = new DBQuery; 599 $q->addTable('custom_fields_lists'); 600 $q->addInsert('field_id', $this->field_id); 601 $q->addInsert('list_option_id', $optid); 602 $q->addInsert('list_value', db_escape(strip_tags($opt))); 603 604 if (!$q->exec()) { 605 $insert_error = $db->ErrorMsg(); 606 } 607 $q->clear(); 608 } 609 //delete the deleted options 610 foreach ($deleteoptions as $opt => $value) { 611 $q = new DBQuery; 612 $q->setDelete('custom_fields_lists'); 613 $q->addWhere('list_option_id =' . $opt); 614 615 if (!$q->exec()) { 616 $delete_error = $db->ErrorMsg(); 617 } 618 $q->clear(); 619 } 620 621 return $insert_error . ' ' . $delete_error; 622 } 623 624 function delete() { 625 $q = new DBQuery; 626 $q->setDelete('custom_fields_lists'); 627 $q->addWhere('field_id = ' . $this->field_id); 628 $q->exec(); 629 $q->clear(); 630 } 631 632 function setOptions($option_array) { 633 $this->options = $option_array; 634 } 635 636 function getOptions() { 637 return $this->options; 638 } 639 640 function itemAtIndex($i) { 641 return $this->options[$i]; 642 } 643 644 function getHTML($field_name, $selected) { 645 $html = '<select name="' . $field_name . '">'; 646 foreach ($this->options as $i => $opt) { 647 $html .= "\t" . '<option value="' . $i . '"'; 648 if ($i == $selected) { 649 $html .= ' selected="selected" '; 650 } 651 $html .= '>' . $opt . '</option>'; 652 } 653 $html .= '</select>'; 654 return $html; 655 } 656 } 657 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 17 03:00:04 2010 | Cross-referenced by PHPXref 0.7 |