<?php
extract($_GET);
extract($_POST);

/**
 * Public page for Classifieds
 */

require "../inc/top.inc.php";
require "../lib/db.class.php";
require "../lib/classifieds.class.php";

if ($PATHS["classifieds-submit"]) {
  list($templateTop, $templateBottom) = plusTemplate($PATHS["classifieds-submit"]);
  print $templateTop;
}

print '<div id="classifieds_ads_admin">';

if (!$what) {
  printf('To delete your ad <a href="%s?what=delete">click here</a>.', $DOCUMENT);
}

$ad = new Ad($_REQUEST['id']);

if ($ad->password && in_array($what, array('save','preview'))) {
  print '<div class="error">You cannot modify saved ad.</div>';

} else {
  switch ($what) {
   case 'delete':
   # -------------------------------------------------------------------------- #
    switch ($ad->delete($_REQUEST['adId'], $_REQUEST['adPassword'])) {
      case 1:
        print '<div class="ok">Your ad was successfully deleted.</div>';
        break;
      case 0:
        print '<div class="error">The identificator and password don\'t match. Please try again.</div>';
      case -1:
        $ad->htmlDelete($_REQUEST['adId'], $_REQUEST['adPassword']);
        break;
    }
    break;
    
   case 'save':
   # -------------------------------------------------------------------------- #
    if (!$ad->save()) {
      print '<div class="error">There was an error and your ad wasn\'t saved. Please send us your ad by e-mail. We apologize for the unconvenience.</div>';
      break;
    }
    print '<div class="ok">Your ad was successfully saved. Please note the following information. You will need it in case you decide to delete your ad later.</div>';
    print '<table id="classifieds_ads_list">';
    printf('<tr><th>Ad #</th><td>%s</td></tr>', $ad->id);
    printf('<tr><th>Password</th><td>%s</td></tr>', $ad->password);
    print '</table>';
    
    /*
     * Send the password by e-mail
     */
    if ($ad->email) {
      $email = ereg_replace("[\r\n]", '', $ad->email);
      $message = <<<KONEC
Dear $ad->name,
please note the following information. You will need it in case you decide to delete your ad later.
Ad #: $ad->id
Password: $ad->password

Best regards
$TITLE
KONEC;
      fromMail("$TITLE <".$SETTINGS['classifieds']['newAdEmail'].">", $email, 'Your ad was successfully saved', $message);
      
    }
    fromMail("$TITLE <".$SETTINGS['classifieds']['newAdEmail'].">", $SETTINGS['classifieds']['newAdEmail'], $TITLE.' - New Classified Ad', 'A new classified ad has been entered.');
    
    print 'Your ad now awaits approval.';
    break;
        
   case 'preview':
   # -------------------------------------------------------------------------- #
    if ($ad->loadForm() && $ad->validate() && $ad->save(true)) {

      $headerFormat = '<tr><th>Title</th><th>Price</th></tr>';
      $adFormat = 
        '<tr><td>%s</td><td class="right">%s</td></tr>' .
        '<tr><td colspan=2 class="contact">%s &nbsp; %s &nbsp; %s &nbsp; %s</td></tr>' .
        '<tr><td colspan=2 class="description">%s%s</td></tr>';
      $ad->setFormat($headerFormat, $adFormat);

      print 'Important: Your ad is not saved until you click Save.';

      print '<div class="head">PREVIEW</div>';

      print '<table id="classifieds_ads_list">';
      print $ad->htmlHeader();
      print $ad->htmlAd();
      print '</table>';

      printf('<form action="%s">', $SERVER['SCRIPT_NAME']);
      print makeFORMITEMhidden(array('id' => $ad->id, 'what'=>'save'));
      print '<div class="right" style="margin-top: 10px;">' .
        '<input type="submit" name="submit" value="Save">' .
        '<input type="button" name="cancel" value="Cancel" onClick="location.href=\''.$DOCUMENT.'\';">' .
        '</div>';
      print '</form>';
    }

   default:
   # -------------------------------------------------------------------------- #
    if ($ad->htmlNew())
      print 'Note: Your ad will be displayed for '.$ad->getTimeLimit().' days.';
    break;
  }
}
print '</div>';
print $templateBottom;
require "../inc/bottom.inc.php";
?>