/** * * @param WOOOF $wo * @param string $id for record to be deleted * @param string $action ('unLike' | 'unHate') * @return id of row deleted */ public static function deleteOpinion(WOOOF $wo, $id, $action) { $place = __CLASS__ . '::' . __FUNCTION__; $wo->debug("{$place}: Delete Movie Opinion"); if (!$wo->hasContent($id)) { $wo->logError(self::_ECP . "1909 No value provided for [id]"); return false; } $tblUserMovieOpinions = new WOOOF_dataBaseTable($wo->db, 'movierama_user_movies_opinions'); if (!$tblUserMovieOpinions->constructedOk) { return false; } //find movie id to update counter $movieOpinionRec = $wo->db->getRow('movierama_user_movies_opinions', $id); if ($movieOpinionRec === FALSE) { return false; } $res = $tblUserMovieOpinions->deleteRow($id); if ($res === FALSE) { return false; } if ($action === 'unLike') { $decreaseLike = self::updateCounter($wo, $movieOpinionRec['movieId'], 'noOfLikes', '-1'); if ($decreaseLike === FALSE) { return false; } } else { if ($action === 'unHate') { $decreaseHate = self::updateCounter($wo, $movieOpinionRec['movieId'], 'noOfHates', '-1'); if ($decreaseHate === FALSE) { return false; } } } return $id; }
/** * * @param string $rowId * @return boolean */ public function deleteRow($rowId, $callLevel = 1) { // CAUTION: infinite recursion is possible! // Static properties: // $deleteRowFilesToRemove; // array( id =>isImage, ... ) // $deleteRowRowIds; // array( table => array( rowId, ... ), ... ) $wo = WOOOF::$instance; $wo->debug("deleteRow: {$this->tableName} [{$rowId}] [{$callLevel}]"); if ($callLevel == 1) { $this::$deleteRowFilesToRemove = array(); $this::$deleteRowRowIds = array(); } if (isset($this::$deleteRowRowIds[$this->tableName][$rowId])) { $wo->logError(self::_ECP . "0530 deleteRow: Detected cycle on {$this->tableName}.{$rowId}"); return true; // break the cycle } // add to memory $this::$deleteRowRowIds[$this->tableName] = $rowId; $theRow = $this->getRow($rowId); if ($theRow === FALSE) { return FALSE; } if ($theRow === NULL) { $wo->logError(self::_ECP . "0540 Delete row [{$rowId}] not found"); return false; } if ($this->hasDeletedColumn) { $res = $this->dataBase->query('update ' . $this->tableName . ' set isDeleted=\'1\' where id=\'' . WOOOF::$instance->cleanUserInput($rowId) . '\''); if ($res === FALSE) { return FALSE; } } else { if ($this->hasGhostTable) { //TODO: ghost table stuf goes here } // antonis // Handle possible File (externalFiles entries) and Picture fields // Remove any relevant external files as well. for ($i = 0; $i < count($this->columns) / 2; $i++) { $metadata = $this->columns[$i]->getColumnMetaData(); if ($metadata['presentationType'] == WOOOF_columnPresentationTypes::picture or $metadata['presentationType'] == WOOOF_columnPresentationTypes::file) { WOOOF_Util::do_dump($metadata['name']); // add to memory $this::$deleteRowFilesToRemove[] = array($theRow[$metadata['name']], $metadata['presentationType'] == WOOOF_columnPresentationTypes::picture); } // file or image field } // for all columns $res = $this->dataBase->query('delete from ' . $this->tableName . ' where id=\'' . WOOOF::$instance->cleanUserInput($rowId) . '\''); if ($res === FALSE) { return FALSE; } } // antonis // Handle possible Detail records // // CAUTION: tablesGroupedByThis should be the space separated list of all tables with groupedByTable == this.table // CAUTION: this is manually done in dbManager // if ($wo->hasContent($this->tablesGroupedByThis)) { $subTableNames = explode(' ', $this->tablesGroupedByThis); foreach ($subTableNames as $aSubTableName) { $subTable = new WOOOF_dataBaseTable($wo->db, $aSubTableName); if (!$subTable->constructedOk) { return FALSE; } if ($subTable->groupedByTable != $this->tableName) { $wo->logError(self::_ECP . "0550 SubTable name mismatch!"); return FALSE; } $res = $subTable->getResult(array($subTable->localGroupColumn => $rowId), null, null, null, null, true, false); if ($res === FALSE) { return FALSE; } foreach ($subTable->resultRows as $aSubTableRow) { $succ = $subTable->deleteRow($aSubTableRow['id'], $callLevel + 1); if ($succ === FALSE) { return FALSE; } } } // foreach subTable } // there exist sub tables if (isset($this->resultRows[$rowId])) { foreach ($this->resultRows as $key => $value) { if ($value['id'] == $rowId) { unset($this->resultRows[$key]); } } } if ($callLevel == 1) { $wo->debug("deleteRow: Completed for all cases."); foreach ($this::$deleteRowFilesToRemove as $aFileData) { $succ = WOOOF_ExternalFiles::deleteExternalFile($wo, $aFileData[0], $aFileData[1]); // ???ignore false results (like failing to delete row from externaal file. } //debug //echo WOOOF_Util::do_dump($this::$deleteRowFilesToRemove); //echo WOOOF_Util::do_dump($this::$deleteRowRowIds); } return TRUE; }
/** * * @param WOOOF $wo * @param id $evaluationId * @param id $requestorUserId * @return bool */ public static function calcResults(WOOOF $wo, $evaluationId, $requestorUserId) { $place = __CLASS__ . '::' . __FUNCTION__; if (!$wo->hasContent($evaluationId)) { $wo->logError(self::_ECP . "5389 No value for evaluation id to calc results"); return false; } $isSelfEvalOwner = self::isEvaluationOwner($wo, $evaluationId, $requestorUserId); if ($isSelfEvalOwner === FALSE) { return false; } if ($isSelfEvalOwner === 0) { $wo->logError(self::_ECP . "5399 I am sorry you cannot request to calc results of the evaluation you are not the owner"); return false; } $tblEvalutionResults = new WOOOF_dataBaseTable($wo->db, 'evaluation_results'); if (!$tblEvalutionResults->constructedOk) { return false; } //first delete all previous results for this evaluationId $res = $tblEvalutionResults->getResult(['evaluationId' => $evaluationId, 'isDeleted' => '0'], '', '', '', '', false, true); if ($res === FALSE) { return false; } foreach ($tblEvalutionResults->resultRows as $aRow) { $res = $tblEvalutionResults->deleteRow($aRow['id']); if ($res === FALSE) { return false; } } //save new results $sql = "select evaluationCriteriaId, avg(theValue) as avg, count(*) as count\n\t\t\tfrom evaluation_answers\n\t\t\twhere evaluationId = '{$evaluationId}'\n\t\t\tgroup by evaluationCriteriaId"; $succ = $wo->db->getResultByQuery($sql, true, false); if ($succ === FALSE) { return FALSE; } foreach ($wo->db->resultRows as $aEvalResult) { $tblEvalResultInsert = new VO_TblEvaluationResults(); $tblEvalResultInsert->evaluationId = $evaluationId; $tblEvalResultInsert->evaluationCriteriaId = $aEvalResult['evaluationCriteriaId']; $tblEvalResultInsert->theValue = $aEvalResult['avg']; $tblEvalResultInsert->theCount = $aEvalResult['count']; $res = self::saveResults($wo, $tblEvalResultInsert); if ($res === FALSE) { return false; } } //update final score and final count on evaluations table $tblEvaluations = new WOOOF_dataBaseTable($wo->db, 'evaluations'); if (!$tblEvaluations->constructedOk) { return false; } //first get the evaluation $evalRow = $wo->db->getRow('evaluations', $evaluationId); $sql = "\n\t\t\tselect\n\t\t\t\tsum(\n\t\t\t\t\tevr.theValue \n\t\t\t\t\t* \n\t\t\t\t\tevc.weight \n\t\t\t\t\t/ \n\t\t\t\t\t(\t\n\t\t\t\t\t\tselect sum(evc2.weight) \n\t\t\t\t\t\tfrom evaluation_criteria evc2 \n\t\t\t\t\t\twhere evc2.evaluationId = '{$evaluationId}'\n\t\t\t\t\t) \n\t\t\t\t) * 10 as finalScore, MAX(evr.theCount) as finalCount\n\t\t\tfrom\n\t\t\t\tevaluation_results evr,\n\t\t\t\tevaluation_criteria evc\n\t\t\twhere\n\t\t\t\tevr.evaluationId = '{$evaluationId}' and\n\t\t\t\tevc.id = evr.evaluationCriteriaId\t\t\n\t\t"; $succ = $wo->db->getResultByQuery($sql, true, false); if ($succ === FALSE) { return FALSE; } foreach ($wo->db->resultRows as $aEval) { $tblEvalUpdate = new VO_TblEvaluations($evalRow); $tblEvalUpdate->finalScore = $aEval['finalScore']; $tblEvalUpdate->finalCount = $aEval['finalCount']; $res = self::save($wo, $tblEvalUpdate); if ($res === FALSE) { return false; } } return true; }
public function cud($in) { $wo = $this->wo; $tableName = $this->tableName; if (!WOOOF::hasContent($tableName)) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1000 No value for tableName"); return false; } $tplContentTop = ''; $tplContent = ''; $tplErrorMessage = ''; $tplMessage = ''; $wo->debug("Generic.cud for '{$tableName}'."); $table = new WOOOF_dataBaseTable($wo->db, $tableName); if (!$table->constructedOk) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1010 Failed to init '{$tableName}'!"); return false; } // form html // $htmlFragment = ''; $id = $wo->getFromArray($in, '_id'); $action = $wo->getFromArray($in, '_action'); $do = $wo->getFromArray($in, '_doAction') == '1'; $redirectToPageAfterSuccess = $wo->getFromArray($in, 'redirectToPageAfterSuccess', ''); $showDetails = $wo->getFromArray($in, '_showDetails') == '1'; if (!WOOOF::hasContent($action)) { if (WOOOF::hasContent($wo->getFromArray($in, '_editButton'))) { $action = 'edit'; } elseif (WOOOF::hasContent($wo->getFromArray($in, '_deleteButton'))) { $action = 'delete'; } elseif (WOOOF::hasContent($wo->getFromArray($in, '_viewButton'))) { $action = 'view'; } } $tplContentTop .= "<h1>'{$tableName}' Form</h1>"; $tplContentTop .= "<h2>[action: {$action}] [id: {$id}] [do: {$do}]</h2>"; // Checks... // if (!in_array($action, array('insert', 'edit', 'delete', 'view'))) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1020 Unknown action [{$action}]"); return false; } // action is valid here... if (!WOOOF::hasContent($id) and $action != 'insert') { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1030 id must be provided for action [{$action}]"); return false; } if (WOOOF::hasContent($id) and $action == 'insert') { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1040 id must not be provided for action [{$action}]"); return false; } if ($action != 'view') { $tplContent .= <<<SOMEHTMLSTRING \t\t\t<form method="post" action="_genericEdit.php" enctype="multipart/form-data"> \t\t\t\t<input type="hidden" name="_tableName" \tvalue="{$tableName}">\t\t\t\t \t\t\t\t<input type="hidden" name="_doAction" \tvalue="1">\t\t\t\t SOMEHTMLSTRING; } $succ = false; $columnsToFill = array(); for ($counter = 0; $counter < count($table->columns) / 2; $counter++) { $metaData = $table->columns[$counter]->getColumnMetaData(); $columnsToFill[] = $metaData['name']; } $submitButtonLabel = 'Save'; $errorsString = ''; // Do the action // if ($do) { if ($action == 'insert') { $succ = $table->handleInsertFromPost($columnsToFill); } elseif ($action == 'edit') { // $_POST['name'] .= 'x'; // demonstrate post-processing / alteration of input $succ = $table->updateRowFromPost($id, $columnsToFill, $errorsString); } elseif ($action == 'delete') { $succ = $table->deleteRow($id); } else { $wo->handleShowStopperError("1050 Unexpected action [{$action}]"); } if ($succ !== FALSE) { $wo->db->commit(); // commit... // TODO: somehow display an ok message (at the page we will redirect) if ($redirectToPageAfterSuccess != '') { header('Location: ' . $redirectToPageAfterSuccess); die; } $tplMessage .= 'Action perfomed ok.'; if ($action == 'insert') { $action = 'edit'; $id = $succ; } } else { // $succ is FALSE $tplErrorMessage .= "Failed to {$action}: " . $wo->getLastErrorAsStringAndClear() . ' ' . $errorsString . '<br>'; $wo->db->rollback(); } // $succ or not } // Draw the data // // TODO: Allow optional htmlFragment param $formBody = ''; if ($action == 'insert') { $formBody = $table->presentRowForInsert(); } elseif ($action == 'edit') { $formBody = $table->presentRowForUpdate($id); } elseif ($action == 'view') { $formBody = $table->presentRowReadOnly($id); } elseif ($action == 'delete') { if (!$do or $succ === FALSE) { $submitButtonLabel = 'Delete'; $formBody = $table->presentRowReadOnly($id); } } else { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1060 Unknown action [{$action}]"); return FALSE; } if ($formBody === FALSE) { $tplContent .= "Failed to perform {$action}!"; $tplErrorMessage .= $wo->getLastErrorAsStringAndClear() . '<br>'; } else { $tplContent .= $formBody; } // Finish up things // if ($action != 'view') { // edit, insert, delete // $action and $id may change after their initial value. $tplContent .= <<<SOMEHTMLSTRING \t\t\t\t<input type="hidden" name="_action" \tvalue="{$action}">\t\t\t\t \t\t\t\t<input type="hidden" name="_id" \t\tvalue="{$id}">\t\t\t\t \t\t\t\t<br> \t\t\t\t<input class="btn btn-medium" type="submit" name="submit" value="{$submitButtonLabel}" > \t\t\t</form> SOMEHTMLSTRING; } if ($action == 'view') { if ($showDetails) { $detailsContent = $this->viewDetailsRecords(array('id' => $id)); if ($detailsContent === FALSE) { return FALSE; } $tplContent .= '<br><br>' . $detailsContent; } else { $tplContent .= '<a href="_genericEdit.php?_tableName=' . $tableName . '&_id=' . $id . '&_action=' . $action . '&_showDetails=1' . '">Show Details Records...</a>' . '<br>'; } } // action is view $tplContent .= <<<SOMEHTMLSTRING \t\t<br> \t\t<a href="_genericList.php?_tableName={$tableName}">Goto '{$tableName}' list...</a> SOMEHTMLSTRING; return array('contentTop' => $tplContentTop, 'content' => $tplContent, 'errorMessage' => $tplErrorMessage, 'message' => $tplMessage); }