Explorar el Código

updated Notify

Piotr Labudda hace 10 años
padre
commit
ee24f4dbe7
Se han modificado 1 ficheros con 67 adiciones y 37 borrados
  1. 67 37
      SE/se-lib/Route/Notify.php

+ 67 - 37
SE/se-lib/Route/Notify.php

@@ -30,18 +30,24 @@ class Route_Notify extends RouteBase {
 		SE_Layout::dol();
 	}
 
-	public function rmAction() {
-		SE_Layout::gora();
-		try {
-			$this->rmUserTableReminder($_GET);
-		} catch (Exception $e) {
-			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
-		}
-		SE_Layout::dol();
-	}
+	public function removeUserTableReminder($args) {
+		$usrLogin = User::getLogin();
+		$who  = V::validate('who',  $args, array('type'=>'word', 'equal'=>$usrLogin, 'error_msg_equal'=>"Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników"));
+		$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('what')));
+		$when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('when')));
 
-	public function rmUserTableReminder($args) {
-		throw new Exception("TODO: rm args:" . json_encode($args));
+		if ($usrLogin != $who) throw new Exception("Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników");
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			delete from `CRM_NOTIFY`
+			where `who` = :who
+				and `what` = :what
+				and `when` = :when
+		");
+		$sth->bindValue(':who', $usrLogin, PDO::PARAM_STR);
+		$sth->bindValue(':what', $what, PDO::PARAM_STR);
+		$sth->bindValue(':when', $when, PDO::PARAM_STR);
+		$sth->execute();
 	}
 
 	public function formUserTableReminder() {
@@ -51,7 +57,21 @@ class Route_Notify extends RouteBase {
 		if ('_add_table_reminder' == $subTask) {
 			try {
 				$this->addUserTableReminder($usrLogin, $_POST);
-				SE_Layout::alert('info', "Dodano przypomnienie");
+				SE_Layout::alert('info', "Dodano powiadomienie");
+			} catch (PDOException $e) {
+				if (!empty($e->errorInfo) && !empty($e->errorInfo[1]) && '1062' == $e->errorInfo[1]) {
+					SE_Layout::alert('warning', "Powiadomienie tego typu zostało już wcześniej dodane");
+				} else {
+					SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage() . '<br>' . "Info: " . json_encode($e->errorInfo));
+				}
+			} catch (Exception $e) {
+				SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+			}
+		}
+		else if ('_rm_table_reminder' == $subTask) {
+			try {
+				$this->removeUserTableReminder($_POST);
+				SE_Layout::alert('info', "Usunięto powiadomienie");
 			} catch (Exception $e) {
 				SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 			}
@@ -60,7 +80,7 @@ class Route_Notify extends RouteBase {
 		?>
 	<h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
 	<?php if (empty($userReminders)) : ?>
-		<?php SE_Layout::alert('warning', "Brak zdefiniowanych przypomnień"); ?>
+		<?php SE_Layout::alert('warning', "Brak zdefiniowanych powiadomień"); ?>
 	<?php else : ?>
 		<table class="table table-hovered">
 			<thead>
@@ -79,9 +99,15 @@ class Route_Notify extends RouteBase {
 						<td><?php echo $this->printValue('when', $reminder['when']); ?></td>
 						<td><?php echo $reminder['_created']; ?></td>
 						<td><?php echo ($reminder['last_exec_time'])? $reminder['last_exec_time'] : '<i>brak danych</i>'; ?></td>
-						<td><a href="index.php?_route=Notify&_task=rm&who=<?php echo $usrLogin; ?>&what=<?php echo $reminder['what']; ?>&when=<?php echo $reminder['when']; ?>"
-									 onclick="return confirm('Czy usunąć przypomnienie?')"
-									 ><i class="glyphicon glyphicon-remove" style="color:red;opacity:0.4;"></i></a></td>
+						<td>
+							<form method="post" class="form-inline">
+								<input type="hidden" name="_subTask" value="_rm_table_reminder">
+								<input type="hidden" name="who" value="<?php echo $usrLogin; ?>">
+								<input type="hidden" name="what" value="<?php echo $reminder['what']; ?>">
+								<input type="hidden" name="when" value="<?php echo $reminder['when']; ?>">
+								<button class="btn btn-link btn-xs" onclick="return confirm('Czy usunąć powiadomienie?')" type="submit"><i class="glyphicon glyphicon-remove" style="color:red;opacity:0.4;"></i></button>
+							</form>
+						</td>
 					</tr>
 				<?php endforeach; ?>
 			</tbody>
@@ -89,18 +115,15 @@ class Route_Notify extends RouteBase {
 	<?php endif; ?>
 
 	<hr>
-	<h4>Dodaj powiadomienia dla wybranych tabel:</h4>
+	<h4>Dodaj powiadomienia:</h4>
 	<form method="post" class="form-inline">
 		<input type="hidden" name="_subTask" value="_add_table_reminder">
 		<label>rodzaj:</label>
 		<select type="select" name="what" class="form-control">
 			<option value="">[ Wybierz ]</option>
-			<option value="l_app_projekty">projekty</option>
-			<option value="l_app_koresp">korespondencja</option>
-			<option value="l_app_zadania">zadania</option>
-			<option value="l_app_procesy">procesy</option>
-			<option value="l_app_zasoby">zasoby</option>
-			<option value="l_app_all_przypomnij">*wszystkie</option>
+			<?php foreach ($this->getFieldValueLabels('what') as $value => $label) : ?>
+				<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
+			<?php endforeach; ?>
 		</select>
 		<label>jak często:</label>
 		<select type="select" name="when" class="form-control">
@@ -124,6 +147,7 @@ class Route_Notify extends RouteBase {
 	public function getFieldValueLabels($fldName) {
 		$valueLabels = array();
 		if ('what' == $fldName) {
+			$valueLabels['msgs'] = "wiadomości";
 			$valueLabels['l_app_projekty'] = "projekty";
 			$valueLabels['l_app_koresp'] = "korespondencja";
 			$valueLabels['l_app_zadania'] = "zadania";
@@ -136,17 +160,15 @@ class Route_Notify extends RouteBase {
 		return $valueLabels;
 	}
 
+	public function getFieldValues($fldName) {
+		$valueLabels = $this->getFieldValueLabels($fldName);
+		return array_keys($valueLabels);
+	}
+
 	public function addUserTableReminder($usrLogin, $args) {
-		$what_values = array();
-		$what_values[] = 'l_app_projekty';
-		$what_values[] = 'l_app_koresp';
-		$what_values[] = 'l_app_zadania';
-		$what_values[] = 'l_app_procesy';
-		$what_values[] = 'l_app_zasoby';
-		$what_values[] = 'l_app_all_przypomnij';
+		$what_values = $this->getFieldValues('what');
 		$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$what_values));
-		$when_values = array();
-		$when_values[] = 'once_a_day';
+		$when_values = $this->getFieldValues('when');
 		$when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
 		$pdo = DB::getPDO();
 		$sth = $pdo->prepare("
@@ -168,7 +190,13 @@ class Route_Notify extends RouteBase {
 		$bindValues['when'] = array($when, PDO::PARAM_STR);
 		$pdo->bindValues($sth, $bindValues);
 		//DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
-		$sth->execute();
+//		try {
+			$sth->execute();
+//		} catch (PDOException $e) {
+//			throw new Exception("Błąd bazy danych: " . json_encode($e->errorInfo));
+//		} catch (Exception $e) {
+//			throw $e;
+//		}
 	}
 
 	public function getUserReminders($usrLogin) {
@@ -185,7 +213,8 @@ class Route_Notify extends RouteBase {
 
 	public function reinstallAction() {
 		try {
-			$this->reinstall();
+			$force = ('true' == V::get('_force', '', $_GET));
+			$this->reinstall($force);
 		} catch (Exception $e) {
 			echo'<div style="border:1px solid red; background:red; color:#fff;">';
 			echo "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage();
@@ -332,7 +361,7 @@ class Route_Notify extends RouteBase {
 		}
 	}
 
-	public function reinstall() {
+	public function reinstall($force = false) {
 		$sqlList = array();
 		$pdo = DB::getPDO();
 		$sqlCreate = <<<EOF_SQL_CREATE
@@ -353,7 +382,7 @@ EOF_SQL_CREATE;
 				- what - action type
 				- last_exec_time
 		*/
-		if(0){// TEST
+		if($force){
 			$pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
 			$pdo->exec("
 					CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
@@ -361,7 +390,8 @@ EOF_SQL_CREATE;
 						, `when` varchar(255) NOT NULL
 						, `what` varchar(255) NOT NULL DEFAULT ''
 						, `last_exec_time` datetime
-						, KEY `who` (`who`)
+						, KEY `key_who` (`who`)
+						, UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
 					) ENGINE=MyISAM DEFAULT CHARSET=latin2
 			");
 		}