patel22p / mytinytodo

Automatically exported from code.google.com/p/mytinytodo
0 stars 0 forks source link

Rename tag #37

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Enter details of feature you request
I wrote a PHP file. It renames the tag, recalculate items tag names.

<?php
echo '<!-- ' . basename(__FILE__) . " -->\n"; 
?>

<html>
<head>
<title>myTinyTodo tag rename</title>
</head>
<body>

<h1>myTinyTodo tag rename</h1>
<?php

$old_tag = $_GET['old_tag'];
$new_tag = $_GET['new_tag'];

$tags = array();

// do rename
if ($old_tag != '' && $new_tag != '')
{
 echo "Rename tag from: '$old_tag' to '$new_tag'<br />";
}

// get all task name
include_once('db/config.php');

$con = mysql_connect($config['mysql.host'], $config['mysql.user'], 
$config['mysql.password']);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($config['mysql.db'], $con);

$sql = 'SELECT id, name FROM ' . $config['prefix'] . 'tags';
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
  $tags[ $row['id'] ] = $row['name'];
  }

// already exists?
$old_id = 0;
$new_exists = false;

foreach ($tags as $id => $name)
 {
 if ($name == $new_tag)
  {
  echo "$new_tag already exists!<br />\n";
  $new_exists = true;
  }

 if ($name == $old_tag)
  {
  $old_id = $id;
  }
 }

if (($old_id != 0) && !$new_exists)
 {
 $tags[$old_id] = $new_tag;

 // rename in tags
 $sql = 'UPDATE ' . $config['prefix'] . "tags SET name='$new_tag' WHERE id='$old_id'";
 mysql_query($sql);

 // rename in todos
 $sql = 'SELECT id, tags_ids FROM ' . $config['prefix'] . 'todolist';
 $result = mysql_query($sql);

 while($row = mysql_fetch_array($result))   // lines from todo
  {
  $todo_id = $row['id'];

  $ids = explode(',', $row['tags_ids']);

  $todo_tags = '';
  foreach ($ids as $tag_id)         // build new tags field
   {
   if ($todo_tags != '')
    $todo_tags .= ',';

   $todo_tags .= $tags[$tag_id];        // from all tags array
   }

  $sql = 'UPDATE ' . $config['prefix'] . "todolist SET tags='$todo_tags' WHERE id='$todo_id'";
  mysql_query($sql);
  } // while

 }  // rename

mysql_close($con);
?>

 <form action="<?php echo basename(__FILE__); ?>" method="get">
    <p>
    <label for="old_tag">Old tag: </label>
              <select id="old_tag" name="old_tag">
  <option>-- choose --</option>

<?php
foreach ($tags as $key => $value)
 {
// echo "<option value=\"$key\">$value</option>\n";
 echo "<option>$value</option>\n";
 }

?>  

</select>
    <label for="new_tag">New tag: </label>
              <input type="text" id="new_tag" name="new_tag"><br />
    <input type="submit" value="Rename"> 
    </p>
 </form>
</body>
</html>

<?php
echo '<!-- /' . basename(__FILE__) . " -->\n";
?>

Original issue reported on code.google.com by blahunka...@gmail.com on 6 Apr 2011 at 9:18