[ Index ]
 

Code source de WebCalendar 1.0.5

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables | Statistiques

title

Body

[fermer]

/ -> purge.php (source)

   1  <?php
   2  /*
   3   * $Id: purge.php,v 1.31 2005/02/22 01:29:58 cknudsen Exp $
   4   *
   5   * Description:
   6   *    Purge events page and handler.
   7   *    When an event is deleted from a user's calendar, it is marked
   8   *    as deleted (webcal_entry_user.cal_status = 'D').  This page
   9   *    will actually clean out the database rather than just mark an
  10   *    event as deleted.
  11   *
  12   * Security:
  13   *    Events will only be deleted if they were created by the selected
  14   *    user.  Events where the user was a participant (but not did not
  15   *    create) will remain unchanged.
  16   *
  17   */
  18  include_once  'includes/init.php';
  19  
  20  // Set this to true do show the SQL at the bottom of the page
  21  $purgeDebug = true;
  22  $sqlLog = '';
  23  
  24  if ( ! $is_admin ) {
  25    // must be admin...
  26    do_redirect ( "index.php" );
  27    exit;
  28  }
  29  
  30  $ALL = 0;
  31  
  32  $purge_all = getPostValue ( "purge_all" );
  33  $end_year = getPostValue ( "end_year" );
  34  $end_month = getPostValue ( "end_month" );
  35  $end_day = getPostValue ( "end_day" );
  36  $user = getPostValue ( "user" );
  37  $preview = getPostValue ( "preview" );
  38  $preview = ( empty ( $preview ) ? false : true );
  39  
  40  $INC = array('js/purge.php','js/visible.php');
  41  if ( ! empty ( $user ) ) {
  42    $BodyX = '';
  43  } else {
  44    $BodyX = 'onload="all_handler();"';
  45  }
  46  
  47  print_header ( $INC, '', $BodyX );
  48  ?>
  49  
  50  <table style="border-width:0px;">
  51  <tr><td style="vertical-align:top; width:50%;">
  52  <?php
  53  echo "<h2>" . translate("Delete Events" );
  54  if ( $preview )
  55    echo "[ " . translate("Preview" ) . "]";
  56  echo "</h2>\n";
  57  
  58  ?>
  59  <a title="<?php etranslate("Admin") ?>" class="nav" href="adminhome.php">&laquo;&nbsp;<?php etranslate("Admin") ?></a><br /><br />
  60  <?php
  61  
  62  if ( ! empty ( $user ) ) {
  63    if ( $preview ) {
  64      echo "<h2> [" .  translate("Preview") . "] " .
  65        translate("Purging events for") . " $user...</h2>\n";
  66    } else {
  67      echo "<h2>" .  translate("Purging events for") . ": $user</h2>\n";
  68    }
  69    $ids = '';
  70    $end_date = sprintf ( "%04d%02d%02d", $end_year, $end_month, $end_day );
  71    if ( $purge_all == "Y" ) {
  72      if ( $user == 'ALL' ) {
  73        $ids = array ( 'ALL' );
  74      } else {
  75        $ids = get_ids (
  76          "SELECT cal_id FROM webcal_entry WHERE cal_create_by = '$user'" );
  77      }
  78    } elseif ( $end_date ) {
  79      if ( $user != 'ALL' ) {
  80        $tail = " AND webcal_entry.cal_create_by = '$user'";
  81      } else {
  82        $tail = '';
  83        $ALL = 1;  // Need this to tell get_ids to ignore participant check
  84      }
  85      $E_ids = get_ids ( "SELECT cal_id FROM webcal_entry " .
  86        "WHERE cal_type = 'E' AND cal_date < '$end_date' $tail",
  87        $ALL );
  88      $M_ids = get_ids ( "SELECT webcal_entry.cal_id FROM webcal_entry " .
  89        "INNER JOIN webcal_entry_repeats ON " .
  90        "webcal_entry.cal_id = webcal_entry_repeats.cal_id " .
  91        "WHERE webcal_entry.cal_type = 'M' AND " .
  92        "cal_end IS NOT NULL AND cal_end < '$end_date' $tail",
  93        $ALL );
  94      $ids = array_merge ( $E_ids, $M_ids );
  95    }
  96    //echo "event ids: <ul><li>" . implode ( "</li><li>", $ids ) . "</li></ul>\n";
  97    if ( count ( $ids ) > 0 ) {
  98      purge_events ( $ids );
  99    } else {
 100      echo translate("None");
 101    }
 102    echo "<h2>..." .  translate("Finished") . ".</h2>\n";
 103    if ( $purgeDebug ) {
 104      echo "<div style=\"border: 1px solid #000;background-color: #fff;\"><tt>$sqlLog</tt></div>\n";
 105    }
 106  } else {
 107  ?>
 108  
 109  <form action="purge.php" method="post" name="purgeform">
 110  <table>
 111      <tr><td><label for="user">
 112          <?php etranslate("User");?>:</label></td>
 113      <td><select name="user">
 114  <?php
 115    $userlist = get_my_users ();
 116    if ($nonuser_enabled == "Y" ) {
 117      $nonusers = get_nonuser_cals ();
 118      $userlist = ($nonuser_at_top == "Y") ? array_merge($nonusers, $userlist) : array_merge($userlist, $nonusers);
 119    }
 120    for ( $i = 0; $i < count ( $userlist ); $i++ ) {
 121      echo "<option value=\"".$userlist[$i]['cal_login']."\">".$userlist[$i]['cal_fullname']."</option>\n";
 122    }
 123  ?>
 124  <option value="ALL" selected="selected"><?php etranslate("All")?></option>
 125          </select>
 126      </td></tr>
 127      <tr><td><label for="purge_all">
 128          <?php etranslate("Check box to delete <b>ALL</b> events for a user")?>:</label></td>
 129          <td valign="bottom">
 130          <input type="checkbox" name="purge_all" value="Y"
 131                  onclick="all_handler()" />
 132      </td></tr>
 133      <tr id="dateArea"><td><label>
 134          <?php etranslate("Delete all events before");?>:</label></td><td>
 135          <?php print_date_selection ( "end_", date ( "Ymd" ) ) ?>
 136      </td></tr>
 137      <tr><td><label for="preview">
 138          <?php etranslate("Preview delete")?>:</label></td>
 139          <td valign="bottom">
 140          <input type="checkbox" name="preview" value="Y" checked="checked" />
 141      </td></tr>
 142      <tr><td colspan="2">
 143          <input type="submit" name="action" value="<?php etranslate("Delete")?>" onclick="return confirm('<?php etranslate("Are you sure you want to delete events for");?> ' + document.forms[0].user.value + '?')" />
 144      </td></tr>
 145  </table>
 146  </form>
 147  
 148  <?php } ?>
 149  </td></tr></table>
 150  
 151  <?php print_trailer(); ?>
 152  </body>
 153  </html>
 154  <?php
 155  function purge_events ( $ids ) {
 156    global $preview, $c; // db connection
 157    global $sqlLog;
 158  
 159    $tables = array (
 160      array ( 'webcal_entry_user', 'cal_id' ),
 161      array ( 'webcal_entry_repeats', 'cal_id' ),
 162      array ( 'webcal_entry_repeats_not', 'cal_id' ),
 163      array ( 'webcal_entry_log', 'cal_entry_id' ),
 164      array ( 'webcal_import_data', 'cal_id' ),
 165      array ( 'webcal_site_extras', 'cal_id' ),
 166      array ( 'webcal_reminder_log', 'cal_id' ),
 167      array ( 'webcal_entry_ext_user', 'cal_id' ),
 168      array ( 'webcal_entry', 'cal_id' )
 169    );
 170  
 171    //var_dump($tables);exit;
 172    $num = array();
 173    for ( $i = 0; $i < count ( $tables ); $i++ ) {
 174      $num[$i] = 0;
 175    }
 176    foreach ( $ids as $cal_id ) {
 177      for ( $i = 0; $i < count ( $tables ); $i++ ) {
 178        $clause = ( $cal_id == 'ALL' ? '' :
 179          " WHERE {$tables[$i][1]} = $cal_id" );
 180        if ( $preview ) {
 181          $sql = "SELECT COUNT(" . $tables[$i][1] .
 182            ") FROM {$tables[$i][0]}" . $clause;
 183          //echo "cal_id = '$cal_id'<br>clause = '$clause'<br>";
 184          //echo "$sql <br/>\n";
 185          $res = dbi_query ( $sql );
 186          $sqlLog .= $sql . "<br/>\n";
 187          if ( $res ) {
 188            if ( $row = dbi_fetch_row ( $res ) )
 189              $num[$i] += $row[0];
 190            dbi_free_result ( $res );
 191          }
 192        } else {
 193          $sql = "DELETE FROM {$tables[$i][0]}" . $clause;
 194          $sqlLog .= $sql . "<br/>\n";
 195          $res = dbi_query ( $sql );
 196          if ( $cal_id == 'ALL' ) {
 197            $num[$i] = translate ( "All" );
 198          } else {
 199            $num[$i] += dbi_affected_rows ( $c, $res );
 200          }
 201        }
 202      }
 203    }
 204    for ( $i = 0; $i < count ( $tables ); $i++ ) {
 205      $table = $tables[$i][0];
 206      echo "[" . translate ( "Preview" ) . "] " .
 207        translate("Records deleted from") .
 208        " $table: $num[$i]<br />\n";
 209    }
 210  }
 211  
 212  function get_ids ( $sql, $ALL = '' ) {
 213    global $sqlLog;
 214    $ids = array ();
 215    //echo "SQL: $sql <br/>\n";
 216    $sqlLog .= $sql . "<br/>\n";
 217    $res = dbi_query ( $sql );
 218    if ( $res ) {
 219      while ( $row = dbi_fetch_row ( $res ) ) {
 220        if ($ALL == 1) {
 221          $ids[] = $row[0];
 222        } else {
 223          //ONLY Delete event if no other participants.
 224          $ID = $row[0];
 225          $res2 = dbi_query ( "SELECT COUNT(*) FROM webcal_entry_user " .
 226            "WHERE cal_id = $ID" );
 227          if ( $res2 ) {
 228            if ( $row2 = dbi_fetch_row ( $res2 ) ) {
 229              if ( $row2[0] == 1 ) $ids[] = $ID;
 230            }
 231            dbi_free_result ( $res2 );
 232          }
 233        } // End if ($ALL)
 234      } // End while
 235    }
 236    dbi_free_result ( $res );
 237    return $ids;
 238  }
 239  ?>


Généré le : Fri Nov 30 19:09:19 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics