submitpub.php 15.9 KB
Newer Older
1 2 3
<?php
#
# Copyright (c) 2000-2008 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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 48
#

include("pub_defs.php");
include("form_defs.php");

#
# Only known and logged in users.
#
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$uid_idx   = $this_user->uid_idx();
$isadmin   = ISADMIN();

#
# Verify feature is enabled
#
if (!$PUBSUPPORT)
    USERERROR("Publication support not enabled.");

#
# Verify page arguments.
#
$optargs = OptionalPageArguments("submit",      PAGEARG_STRING,
			  	 "idx", 	PAGEARG_INTEGER,
                                 "formfields",  PAGEARG_ARRAY);

49 50 51 52 53
#
# Standard Testbed Header
#
PAGEHEADER("Submit Publication");

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
$also_required = array();
$also_required['article'] = array('conf', 'month');

$typelist = array('article', 'phd thesis', 'masters thesis', 'bachelors thesis', 
                  'techreport', 'unpublished', 'talk', 'software', 'service');

$submit_url = isset($idx) ? CreateURL("submitpub", 'idx', $idx) : CreateURL("submitpub");

#
# Make Month List
#
$monthlist = array();
$query_result = DBQueryFatal("select * from emulab_pubs_month_map order by display_order");
while ($r = mysql_fetch_array($query_result)) {
  $monthlist[$r['month']] = $r['month_name'];
}

#
# Make Project List
#
$projectlist = array();
if ($isadmin) {
  $query_result = DBQueryFatal("select pid from projects order by pid");
} else {
  $query_result = DBQueryFatal("select distinct pid ".
			       "  from group_membership ".
			       "  where uid='$uid' order by pid");
}
while ($r = mysql_fetch_array($query_result)) {
    array_push($projectlist, $r[0]);   
}
array_push($projectlist, "N/A");

#
# Make Category List
#
$categorylist = array();
$query_result = DBQueryFatal("select distinct category ".
			   "  from emulab_pubs order by category");
while ($r = mysql_fetch_array($query_result)) {
    if ($r[0] != "") 
	array_push($categorylist, $r[0]);   
}
array_push($categorylist, "Other");

$form = array('#action' => $submit_url);
$default_textfield_size = 60;

#
# Create Fields List
#
function WrapOptionsList($options) {
    $res = array();
    foreach ($options as $o) {
        $res[$o] = $o;
    }
    return $res;
}
$fields = array();
if ($isadmin && isset($idx) && !isset($submit)) {
  $fields['created'] =
    array('#type' => 'display',
          '#label' => 'Created');
  $fields['owner_name'] =
    array('#type' => 'display',
          '#label' => 'Owner');
  $fields['submitted_by_name'] =
    array('#type' => 'display',
          '#label' => 'Submitted By');
  $fields['last_edit'] =
    array('#type' => 'display',
          '#label' => 'Last Edit');
  $fields['last_edit_by_name'] = 
   array('#type' => 'display',
         '#label' => 'Last Edit By');
}
$fields['type'] =
    array('#type' => 'select',
          '#label' => 'Type',
          '#required' => true,
	  '#checkslot' => 'default:tinytext',
          '#options' => WrapOptionsList($typelist));
$fields['authors'] = 
    array('#type' => 'textfield',
          '#label' => 'Authors',
	  '#size' => $default_textfield_size,
          '#required' => true,
141
	  '#prep' => 'trim',
142 143 144 145 146 147 148
	  '#checkslot' => 'default:tinytext_utf8',
          '#description' => 'comma seperated list');
$fields['affil'] = 
    array('#type' => 'textfield',
          '#label' => 'Author Affiliations',
	  '#size' => $default_textfield_size,
          '#required' => true,
149
	  '#prep' => 'trim',
150 151 152 153 154 155 156
	  '#checkslot' => 'default:tinytext_utf8',
          '#description' => 'comma separated list, only list each institution once');
$fields['title'] = 
    array('#type' => 'textfield',
          '#label' => 'Title',
	  '#size' => $default_textfield_size,
          '#required' => true,
157
	  '#prep' => 'trim',
158 159 160 161 162 163
	  '#checkslot' => 'default:tinytext_utf8');
$fields['conf'] = 
    array('#type' => 'textfield',
          '#label' => 'Conference/Journal',
	  '#size' => $default_textfield_size,
          '#required' => false,
164
	  '#prep' => 'trim',
165 166 167 168 169 170
	  '#checkslot' => 'default:tinytext_utf8');
$fields['where'] = 
    array('#type' => 'textfield',
          '#label' => 'Conference Location',
	  '#size' => $default_textfield_size,
          '#required' => false,
171
	  '#prep' => 'trim',
172 173 174 175 176 177 178
	  '#checkslot' => 'default:tinytext_utf8');
$fields['year'] = 
    array('#type' => 'textfield',
          '#label' => 'Year',
          '#size' => 4,
	  '#maxlength' => 4,
          '#required' => true,
179
	  '#prep' => 'trim',
180 181 182 183 184 185 186 187 188 189 190 191 192
	  '#checkslot' => '/^\d\d\d\d$/');
$fields['month'] = 
    array('#type' => 'select',
          '#label' => 'Month',
          '#required' => false,
	  '#no_default' => true,
          '#options' => $monthlist,
	  '#checkslot' => 'default:tinytext');
$fields['volume'] = 
    array('#type' => 'textfield',
          '#label' => 'Volume',
	  '#size' => $default_textfield_size,
          '#required' => false,
193
	  '#prep' => 'trim',
194 195 196 197 198 199
	  '#checkslot' => 'default:tinytext');
$fields['number'] = 
    array('#type' => 'textfield',
          '#label' => 'Number',
	  '#size' => $default_textfield_size,
          '#required' => false,
200
	  '#prep' => 'trim',
201 202 203 204 205 206
	  '#checkslot' => 'default:tinytext');
$fields['pages'] = 
    array('#type' => 'textfield',
          '#label' => 'Pages',
	  '#size' => $default_textfield_size,
          '#required' => false,
207
	  '#prep' => 'trim',
208 209 210 211 212 213
	  '#checkslot' => 'default:tinytext');
$fields['url'] = 
    array('#type' => 'textfield',
          '#label' => 'Publication URL',
	  '#size' => $default_textfield_size,
          '#required' => true,
214
	  '#prep' => 'trim',
215 216 217 218 219 220
	  '#checkslot' => 'default:tinytext');
$fields['conf_url'] = 
    array('#type' => 'textfield',
          '#label' => 'Conference URL',
	  '#size' => $default_textfield_size,
          '#required' => false,
221
	  '#prep' => 'trim',
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	  '#checkslot' => 'default:tinytext');
$fields['category'] = 
    array('#type' => 'vlist',
          '#label' => 'Category',
          '#required' => true,
          '#elements' => array(
              'category' => 
                  array('#type' => 'select',
                        '#required' => true,
                        '#options' => WrapOptionsList($categorylist),
	                '#checkslot' => 'default:tinytext'),
              'category_other' =>
                  array('#type' => 'textfield',
                        '#label' => 'If other specify',
 	  		'#size' => 50,
237
	  		'#prep' => 'trim',
238 239 240 241 242 243 244 245 246
	                '#checkslot' => 'default:tinytext',
			'#nodump' => true)));
if ($isadmin) {
    $fields['evaluated_on_emulab'] = 
        array('#type' => 'textfield',
             '#label' => 'Evaluated on emulab, or just cite?',
             '#description' => '"Y" or "N"',
	     '#size' => $default_textfield_size,
             '#required' => false,
247
	     '#prep' => 'trim',
248 249 250 251 252 253 254 255 256 257 258 259
  	     '#checkslot' => 'default:tinytext');
}
$fields['project'] = 
    array('#type' => 'select',
          '#label' => 'Project',
          '#required' => 'true',
          '#size' => $default_textfield_size,
          '#options' => WrapOptionsList($projectlist),
          '#checkslot' => 'default:tinytext');
$fields['cite_osdi02'] = 
    array('#type' => 'vlist',
          '#label' => "Did you cite our OSDI'02 paper?",
260
	  '#link' => "$WIKIDOCURL/AdminPolicies#reporting",
261 262 263 264 265 266 267 268 269 270 271
          '#required' => true,
	  '#elements' => array(
              'cite_osdi02' =>
                  array('#type' => 'select',
                        '#required' => true,
			'#options' => array(1 => 'yes', 0 => 'no'),
                        '#checkslot' => 'default:boolean'),
              'no_cite_why' =>
                  array('#label' => 'If (no) why not',
			'#type' => 'textfield',
                        '#size' => 50,
272
			'#prep' => 'trim',
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
                        '#checkslot' => 'default:tinytext_utf8')));
if ($isadmin) {
    $fields['visible'] = 
        array('#type' => 'checkbox',
              '#label' => "Visible in Public Bibliography",
              '#checkslot' => 'default:boolean');
    $fields['editable_owner'] =
        array('#type' => 'checkbox',
              '#label' => "Editable by Owner",
              '#checkslot' => 'default:boolean');
    $fields['editable_proj'] =
        array('#type' => 'checkbox',
              '#label' => "Editable by Project Members",
              '#checkslot' => 'default:boolean');
    #TableRow("Take Ownership", "ownership", true,
    #         CheckBox("ownership") . "<br>".
    #         "<font size=-1><i>Show Publication in My Emulab Page?</i></font>");
}
$fields['notes'] = 
    array('#type' => 'textarea',
          '#label' => 'Additional Notes',
          '#required' => false,
	  '#checkslot' => 'default:fulltext_utf8',
          '#cols' => 78,
          '#rows' => 2);
if (isset($idx)) {
    $fields['deleted'] =
        array('#type' => 'checkbox',
              '#label' => 'Mark As Deleted');
}

$dbfields = array();
foreach ($fields as $k => $v) {
    if ($v['#type'] == 'display') continue;
    array_push($dbfields, $k);
}
array_push($dbfields, 'no_cite_why');

if ($isadmin) {
  $fields['allow_missing'] =
      array('#type' => 'checkbox',
	    '#label' => 'Allow Missing Fields');
}

#
# Spit the form out using the array of data and error strings (if any).
# 
function SPITFORM($formfields, $errors)
{
    global $form, $fields;

    echo "<center>";
    FormRender($form, $errors, $fields, $formfields);
    echo "</center>";
}

#
#
#

$defaults = array();
$errors  = array();

#
# Helper functions
#
function field_set($f) {
  global $formfields;
  return isset($formfields[$f]) && $formfields[$f] != "";
}

function GetName($what, $skip_if_same_as = null) {
  global $fields;
  global $defaults;
  $field = $what . "_name";
  if ($skip_if_same_as != null && $defaults[$what] == $defaults[$skip_if_same_as]) {
    unset($fields[$field]);
  } else if ($defaults[$what] == 0) {
    $fields[$field]['#value'] = 'Nobody';
  } else {
    $user = new User($defaults[$what]);
    $fields[$field]['#value'] = $user->name() . " (" . $user->uid() . ")";
  }
}


if (isset($idx)) {

  $query_result = DBQueryFatal("select * from emulab_pubs where idx=$idx");
  if (mysql_num_rows($query_result) < 1) {
    echo "Pub #$idx does not exist\n";
    PAGEFOOTER();
    return;
  }
  $r = mysql_fetch_assoc($query_result);
  if (!MayEditPub($this_user, $isadmin, $r)) {
    echo "You Do Not Have Permission to Edit this Publication Submission\n";
    PAGEFOOTER();
    return;
  }

  $defaults = $r;

  if ($isadmin && !isset($submit)) {
    GetName('submitted_by');
    GetName('owner', 'submitted_by');
    GetName('last_edit_by', 'submitted_by');
    if ($defaults['created'] == $defaults['last_edit'])
      unset($fields['last_edit']);
  }

  $fields['submit'] =
      array('#type' => 'submit',
	    '#value' => "Update");

} else {

390 391
  if (!$isadmin)
    $defaults['project'] = $projectlist[0];
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  $defaults['visible'] = true;
  $defaults['editable_owner'] = true;
  $defaults['editable_proj'] = true;
  #$defaults['ownership'] = false;

  $fields['submit'] =
      array('#type' => 'submit',
	    '#value' => "Submit");

}

if (!isset($submit)) {

  SPITFORM($defaults, $errors);

  PAGEFOOTER();
  return;

} 

#
# Verify Form
# 
415 416
if ((isset($formfields['deleted']) && $formfields['deleted'])
    || (isset($formfields['allow_missing']) && $formfields['allow_missing'])) {
417 418 419 420 421 422 423 424
    # Hack, modify fields to unset required flag
    foreach ($fields as $k => $v) {
	unset($fields[$k]['#required']);
    }
    unset($fields['category']['#elements']['category']['#required']);
    unset($fields['cite_osdi02']['#elements']['cite_osdi02']['#required']);
}

425 426 427 428 429
# hack since '' == 0 will evaluate to true and thus, "No" 
# will incorrectly be selected
if (strcmp($formfields['cite_osdi02'], '') == 0)
    unset($formfields['cite_osdi02']);

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
FormValidate($form, $errors, $fields, $formfields);

if (!(isset($formfields['allow_missing']) && $formfields['allow_missing'])) {

    $type = $formfields['type'];
    if (isset($also_required[$type])) {
      foreach ($also_required[$type] as $f) {
	if (!field_set($f)) {
	  $errors[$fields[$f]['#label']] = "Required for $type type.";
	}
      }
    }

    if (field_set('cite_osdi02') && !$formfields['cite_osdi02'] && !field_set('no_cite_why')) {
      $errors[$fields['cite_osdi02']['#label']] = "Must specify reason.";
    }
}

if ($formfields['category'] == 'Other') {
  if (field_set('category_other')) {
    $formfields['category'] = $formfields['category_other'];
  } else {
    $errors[$fields['category']['#label']] = "Must specify.";
  }
}

$formfields['notes'] = str_replace("\r", "", $formfields['notes']);

if ($errors) {
  SPITFORM($formfields, $errors);
  PAGEFOOTER();
  return;
}

#
# 
#
$formfields['month_name'] = $monthlist[$formfields['month']];

#
# Build DB Query and Send Mail to Testbed-ops
#

$formdump = FormTextDump($form, $fields, $formfields, 30);

function ConfirmationCommon($deleted = false) {
    global $form, $fields, $formfields, $formdump, $idx;

Kevin Atkinson's avatar
Kevin Atkinson committed
478
    echo "<pre>\n". htmlspecialchars($formdump). "\n</pre>\n";
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

    if (!$deleted) {
	echo '<p>It will appear in the public <a href="expubs.php">Bibliography</a> ';
	echo "under ". htmlspecialchars($formfields['category']) ." like this:</p>";

	echo "<ul>\n<li>\n";
	echo MakeBib(NULL, 0, $formfields);
	echo "</li></ul>\n";
    }

    echo "<a href=\"submitpub.php?idx=". $idx . "\" style=\"background:yellow\">Edit</a>";
}

if (!isset($idx)) {

    $cols = array('owner', 'submitted_by', 'last_edit_by', 'uuid', 'created', 'last_edit');
    #$owner = $isadmin && !$formfields['ownership'] ? 0 : $uid_idx;
    $owner = $uid_idx;
    $vals = array($owner, $uid_idx, $uid_idx, '"'.mysql_escape_string(NewUUID()).'"', "now()", "now()");
    foreach ($dbfields as $f) {
499 500 501 502 503 504
      if (isset($formfields[$f])) {
        $dbname = $f;
        $value  = $formfields[$f];
        array_push($cols, '`'. $dbname . '`');
        array_push($vals, '"'. mysql_escape_string($value) . '"');
      }
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    }

    DBQueryFatal("insert into emulab_pubs (".
		 implode(",", $cols). ") values (".
		 implode(",", $vals). ")");

    $idx = mysql_insert_id();

    echo "<p>The following  was Submitted: </p>";

    ConfirmationCommon();

    if (!$isadmin) {
        TBMAIL("$TBMAILADDR_OPS",
               "New Publication Submitted",
               $formdump,
               "From: ". $this_user->name() . "<" . $this_user->email() . ">");
    }

    PAGEFOOTER();

} else {

    $update_list = array("last_edit = now()", "`last_edit_by` = $uid_idx");
    $update_start = count($update_list);

    if (!isset($formfields['deleted'])) 
      $formfields['deleted'] = false;

    # determine what changed
    foreach ($dbfields as $f) {
536
      if (isset($formfields[$f]) && $defaults[$f] != $formfields[$f]) {
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
        $dbname = $f;
        $value  = @$formfields[$f];
        array_push($update_list,
	           "`".$dbname."` = \"".
                   mysql_escape_string($value) . '"');
      }
    }

    if (count($update_list) > $update_start) {

      DBQueryFatal("update emulab_pubs set ".
                   implode(",", $update_list).
                   " where idx = $idx");
      
      if (!$formfields['deleted']) {

        echo "<p>The following publication was Updated:</p>\n";

        ConfirmationCommon();

      } else {

        echo "<p>The following publication was deleted.  ";
        echo "To undelete simply edit it again and unclick \"Mark As Deleted\"</p>\n";

        ConfirmationCommon(true);

      }

    } else {

      echo "<p>Nothing has changed for the following pub:</p>";

      ConfirmationCommon();
 
    }

    PAGEFOOTER();
}

?>