diff --git a/database/factories/TaskFactory.php b/database/factories/TaskFactory.php index 992afea7c..13dd74abf 100644 --- a/database/factories/TaskFactory.php +++ b/database/factories/TaskFactory.php @@ -1,4 +1,5 @@ define(\ProcessMaker\Model\Task::class, function(Faker $faker) { return $process->PRO_UID; }, 'TAS_UID' => G::generateUniqueID(), - 'TAS_ID' => $faker->unique()->numberBetween(1, 2000), + 'TAS_ID' => $faker->unique()->numberBetween(1, 100000), 'TAS_TITLE' => $faker->sentence(2), 'TAS_TYPE' => 'NORMAL', 'TAS_TYPE_DAY' => 1, @@ -31,4 +32,4 @@ $factory->define(\ProcessMaker\Model\Task::class, function(Faker $faker) { 'TAS_CAN_SEND_MESSAGE' => 'FALSE', 'TAS_SEND_LAST_EMAIL' => 'FALSE', ]; -}); \ No newline at end of file +}); diff --git a/tests/unit/workflow/engine/classes/ReportTablesTest.php b/tests/unit/workflow/engine/classes/ReportTablesTest.php new file mode 100644 index 000000000..c48ed8191 --- /dev/null +++ b/tests/unit/workflow/engine/classes/ReportTablesTest.php @@ -0,0 +1,733 @@ +prepareData($tableName, 1); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + $proUid = $result->processUid; + $grid = ''; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid, $grid); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals($expected, $actual); + } + + /** + * Check if the "populateTable" function returns an array value if entered all + * parameters and type and grid are correct values. + * @test + * @covers ReportTables::populateTable + */ + public function it_should_populating_data_with_all_parameters_with_type_is_grid() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 18, true); + $connectionShortName = 'wf'; + $type = 'GRID'; + $fields = $result->fields; + $proUid = $result->processUid; + $grid = 'var_Grid1'; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid, $grid); + + $indexRow = 1; + $expected = $result->appData[$grid]; + foreach ($expected as &$row) { + $row['APP_UID'] = $result->applicationUid; + $row['APP_NUMBER'] = $result->applicationNumber; + $row['ROW'] = (string) ($indexRow++); + } + $expected = array_values($expected); + + $actual = DB::table($tableName) + ->select() + ->get(); + $actual->transform(function ($item, $key) { + return (array) $item; + }); + $actual = $actual->toArray(); + + $this->assertEquals($expected, $actual); + } + + /** + * Check if the "populateTable" function returns an array value if entered all + * parameters and type and grid are incorrect values. + * @test + * @covers ReportTables::populateTable + */ + public function it_should_populating_data_with_all_parameters_with_type_is_grid_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 19, true); + $connectionShortName = 'wf'; + $type = 'GRID'; + $fields = $result->fields; + $proUid = $result->processUid; + $grid = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid, $grid); + + $actual = DB::table($tableName) + ->select() + ->get(); + $actual->transform(function ($item, $key) { + return (array) $item; + }); + $actual = $actual->toArray(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if only the + * name of the report table has been entered. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_only_with_the_mandatory_parameter_tableName() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 2); + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable and the name of the + * connection. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_connectionShortName_parameter() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 3); + $connectionShortName = 'wf'; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable and the name of the + * connection is null. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_connectionShortName_parameter_is_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 8); + $connectionShortName = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable and the name of the + * connection is incorrect value. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_connectionShortName_parameter_is_incorrect_value() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 9); + $connectionShortName = G::generateUniqueID(); + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection and type. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 4); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection and type is grid. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_is_grid() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 11); + $connectionShortName = 'wf'; + $type = 'GRID'; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection and type is null. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_is_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 10); + $connectionShortName = 'wf'; + $type = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type and fields. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 5); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type and fields is null. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_is_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 12); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type and fields is empty array. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_is_empty_array() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 13); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = []; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type and fields is incorrect value. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_is_incorrect_value() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 14); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = ""; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an array value if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type, the fields and process identifier. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_proUid() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 6); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + $proUid = $result->processUid; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals($expected, $actual); + } + + /** + * Check if the "populateTable" function returns an empty array if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type, the fields and process identifier is null. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_proUid_is_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 15); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + $proUid = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals([], $actual); + } + + /** + * Check if the "populateTable" function returns an array value if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type, the fields, the process identifier and grid name. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_proUid_grid() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 7); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + $proUid = $result->processUid; + $grid = ''; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid, $grid); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals($expected, $actual); + } + + /** + * Check if the "populateTable" function returns an array value if you have + * entered the name of the table of the reportTable, the name of the + * connection, the type, the fields, the process identifier and grid name if null. + * @test + * @covers ReportTables::populateTable + */ + public function this_should_populate_the_reports_table_with_the_parameters_connectionShortName_type_fields_proUid_grid_if_null() + { + $tableName = 'TestReportTable'; + $result = $this->prepareData($tableName, 16); + $connectionShortName = 'wf'; + $type = 'NORMAL'; + $fields = $result->fields; + $proUid = $result->processUid; + $grid = null; + + $reportTables = new ReportTables(); + $reportTables->populateTable($tableName, $connectionShortName, $type, $fields, $proUid, $grid); + + $expected = $result->dataFields; + $expected['APP_UID'] = $result->applicationUid; + $expected['APP_NUMBER'] = $result->applicationNumber; + + $actual = (array) DB::table($tableName) + ->select() + ->first(); + + $this->assertEquals($expected, $actual); + } + + /** + * Get mapping fields supported by report table. + * @return array + */ + private function getMapFields() + { + return [ + [ + 'sFieldName' => 'var_Text1', + 'sType' => 'char' + ], + [ + 'sFieldName' => 'var_Textarea1', + 'sType' => 'text' + ], + [ + 'sFieldName' => 'var_Dropdown1', + 'sType' => 'char' + ], + [ + 'sFieldName' => 'var_Suggest1', + 'sType' => 'char' + ], + [ + 'sFieldName' => 'var_DateTime1', + 'sType' => 'date' + ], + [ + 'sFieldName' => 'var_String1', + 'sType' => 'char' + ], + [ + 'sFieldName' => 'var_Integer1', + 'sType' => 'number' + ], + [ + 'sFieldName' => 'var_Boolean1', + 'sType' => 'boolean' + ], + [ + 'sFieldName' => 'var_Array1', + 'sType' => 'array' + ] + ]; + } + + /** + * Create fields data by type supported. + * @param array $types + * @return array + */ + private function createFieldsByType($types = []) + { + $fields = []; + $mapping = []; + $faker = Faker\Factory::create(); + $date = $faker->dateTime(); + $mapFields = $this->getMapFields(); + foreach ($mapFields as $key => $value) { + if (!in_array($value['sType'], $types)) { + continue; + } + switch ($value['sType']) { + case 'number': + $mapping[] = $value; + $fields[$value['sFieldName']] = (string) random_int(0, 100); + break; + case 'char': + $mapping[] = $value; + $fields[$value['sFieldName']] = G::generateUniqueID(); + break; + case 'text': + $mapping[] = $value; + $fields[$value['sFieldName']] = G::generateUniqueID(); + break; + case 'date': + $mapping[] = $value; + $fields[$value['sFieldName']] = $date->format('Y-m-d H:i:s'); + break; + case 'boolean': + $mapping[] = $value; + $fields[$value['sFieldName']] = ['0' => 0]; + break; + } + } + return [ + 'data' => $fields, + 'mapping' => $mapping + ]; + } + + /** + * Prepare data initial for test, the grid parameter is optional if you want + * to create a grid type field. + * + * @param string $tableName + * @param integer $applicationNumber + * @param boolean $grid + * @return object + */ + private function prepareData($tableName, $applicationNumber, $grid = null) + { + $faker = Faker\Factory::create(); + $date = $faker->dateTime(); + + $userUid = G::generateUniqueID(); + $processUid = G::generateUniqueID(); + $taskUid = G::generateUniqueID(); + $applicationUid = G::generateUniqueID(); + + $structure = $this->createFieldsByType(['number', 'char', 'text', 'date']); + $fields = $structure['mapping']; + $dataFields = $structure['data']; + $appData = [ + 'SYS_LANG' => 'en', + 'SYS_SKIN' => 'neoclassic', + 'SYS_SYS' => 'workflow', + 'APPLICATION' => G::generateUniqueID(), + 'PROCESS' => G::generateUniqueID(), + 'TASK' => '', + 'INDEX' => 2, + 'USER_LOGGED' => $userUid, + 'USR_USERNAME' => 'admin', + 'APP_NUMBER' => $applicationNumber, + 'PIN' => '97ZN' + ]; + $appData = array_merge($appData, $dataFields); + if ($grid === true) { + $gridFields = [ + 'var_Grid1' => [ + '1' => $dataFields, + '2' => $dataFields, + ] + ]; + $appData = array_merge($appData, $gridFields); + } + + $user = factory(User::class)->create([ + 'USR_UID' => $userUid + ]); + + $process = factory(Process::class)->create([ + 'PRO_UID' => $processUid + ]); + + $task = factory(Task::class)->create([ + 'PRO_UID' => $process->PRO_UID + ]); + + $application = factory(Application::class)->create([ + 'PRO_UID' => $process->PRO_UID, + 'APP_UID' => $applicationUid, + 'APP_NUMBER' => $applicationNumber, + 'APP_DATA' => serialize($appData) + ]); + + Schema::dropIfExists($tableName); + Schema::create($tableName, function ($table) use ($dataFields, $grid) { + $table->string('APP_UID'); + $table->string('APP_NUMBER'); + if ($grid === true) { + $table->string('ROW'); + } + foreach ($dataFields as $key => $value) { + $table->string($key); + } + }); + $result = new stdClass(); + $result->userUid = $userUid; + $result->processUid = $processUid; + $result->taskUid = $taskUid; + $result->applicationUid = $applicationUid; + $result->applicationNumber = $applicationNumber; + $result->fields = $fields; + $result->dataFields = $dataFields; + $result->appData = $appData; + $result->user = $user; + $result->process = $process; + $result->task = $task; + $result->application = $application; + return $result; + } +} diff --git a/workflow/engine/classes/ReportTables.php b/workflow/engine/classes/ReportTables.php index 1f4e419cd..2bdea2167 100644 --- a/workflow/engine/classes/ReportTables.php +++ b/workflow/engine/classes/ReportTables.php @@ -1,7 +1,10 @@ processConsolidated() + * @see Processes->createReportTables() + * @see workflow/engine/methods/cases/caseConsolidated.php + * @see workflow/engine/methods/processes/consolidated.php ajax_con->con_save_properties() + * @see workflow/engine/methods/reportTables/reportTables_Save.php + * @link https://wiki.processmaker.com/3.0/Report_Tables * @return void */ - public function populateTable( - $sTableName, - $sConnection = 'report', - $sType = 'NORMAL', - $aFields = array(), - $sProcessUid = '', - $sGrid = '' - ) + public function populateTable($tableName, $connectionShortName = 'report', $type = 'NORMAL', $fields = [], $proUid = '', $grid = '') { - $sTableName = $this->sPrefix . $sTableName; + $tableName = $this->sPrefix . $tableName; //we have to do the propel connection - $PropelDatabase = $this->chooseDB($sConnection); - $con = Propel::getConnection($PropelDatabase); - $stmt = $con->createStatement(); - if ($sType == 'GRID') { - $aAux = explode('-', $sGrid); - $sGrid = $aAux[0]; + $database = $this->chooseDB($connectionShortName); + $connection = Propel::getConnection($database); + $statement = $connection->createStatement(); + if ($type == 'GRID') { + $aux = explode('-', $grid); + $grid = $aux[0]; } + $case = new Cases(); try { switch (DB_ADAPTER) { case 'mysql': - //select cases for this Process, ordered by APP_NUMBER - $oCriteria = new Criteria('workflow'); - $oCriteria->add(ApplicationPeer::PRO_UID, $sProcessUid); - $oCriteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER); - $oDataset = ApplicationPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - while ($aRow = $oDataset->getRow()) { - $aData = unserialize($aRow['APP_DATA']); - //delete previous record from this report table ( previous records in case this is a grid ) - $deleteSql = 'DELETE FROM `' . $sTableName . "` WHERE APP_UID = '" . $aRow['APP_UID'] . "'"; - $rsDel = $stmt->executeQuery($deleteSql); - if ($sType == 'NORMAL') { - $sQuery = 'INSERT INTO `' . $sTableName . '` ('; - $sQuery .= '`APP_UID`,`APP_NUMBER`'; - foreach ($aFields as $aField) { - $sQuery .= ',`' . $aField['sFieldName'] . '`'; + $applications = Application::getByProUid($proUid); + foreach ($applications as $application) { + $appData = $case->unserializeData($application->APP_DATA); + DB::delete("DELETE FROM `{$tableName}` WHERE APP_UID = '{$application->APP_UID}'"); + if ($type == 'NORMAL') { + $query = 'INSERT INTO `' . $tableName . '` ('; + $query .= '`APP_UID`,`APP_NUMBER`'; + foreach ($fields as $field) { + $query .= ',`' . $field['sFieldName'] . '`'; } - $sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER']; - foreach ($aFields as $aField) { - switch ($aField['sType']) { + $query .= ") VALUES ('" . $application->APP_UID . "'," . $application->APP_NUMBER; + foreach ($fields as $field) { + switch ($field['sType']) { case 'number': - $sQuery .= ',' . (isset($aData[$aField['sFieldName']]) ? (float)str_replace( - ',', - '', - $aData[$aField['sFieldName']] - ) : '0'); + $query .= ',' . (isset($appData[$field['sFieldName']]) ? (float) str_replace(',', '', $appData[$field['sFieldName']]) : '0'); break; case 'char': case 'text': - if (!isset($aData[$aField['sFieldName']])) { - $aData[$aField['sFieldName']] = ''; + if (!isset($appData[$field['sFieldName']])) { + $appData[$field['sFieldName']] = ''; } - $sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? mysqli_real_escape_string( - $con->getResource(), - $aData[$aField['sFieldName']] - ) : '') . "'"; + $string = $appData[$field['sFieldName']]; + if (is_array($string)) { + $string = implode($string, ","); + } + $query .= ",'" . (isset($appData[$field['sFieldName']]) ? mysqli_real_escape_string($connection->getResource(), $string) : '') . "'"; break; case 'date': - $value = (isset($aData[$aField['sFieldName']]) && trim($aData[$aField['sFieldName']])) != '' ? "'" . $aData[$aField['sFieldName']] . "'" : 'NULL'; - $sQuery .= "," . $value; + $value = (isset($appData[$field['sFieldName']]) && trim($appData[$field['sFieldName']])) != '' ? "'" . $appData[$field['sFieldName']] . "'" : 'NULL'; + $query .= "," . $value; break; } } - $sQuery .= ')'; - $rs = $stmt->executeQuery($sQuery); + $query .= ')'; + DB::insert($query); } else { - if (isset($aData[$sGrid])) { - foreach ($aData[$sGrid] as $iRow => $aGridRow) { - $sQuery = 'INSERT INTO `' . $sTableName . '` ('; - $sQuery .= '`APP_UID`,`APP_NUMBER`,`ROW`'; - foreach ($aFields as $aField) { - $sQuery .= ',`' . $aField['sFieldName'] . '`'; + if (isset($appData[$grid])) { + foreach ($appData[$grid] as $indexRow => $gridRow) { + $query = 'INSERT INTO `' . $tableName . '` ('; + $query .= '`APP_UID`,`APP_NUMBER`,`ROW`'; + foreach ($fields as $field) { + $query .= ',`' . $field['sFieldName'] . '`'; } - $sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'] . ',' . $iRow; - foreach ($aFields as $aField) { - switch ($aField['sType']) { + $query .= ") VALUES ('" . $application->APP_UID . "'," . (int) $application->APP_NUMBER . ',' . $indexRow; + foreach ($fields as $field) { + switch ($field['sType']) { case 'number': - $sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace( - ',', - '', - $aGridRow[$aField['sFieldName']] - ) : '0'); + $query .= ',' . (isset($gridRow[$field['sFieldName']]) ? (float) str_replace(',', '', $gridRow[$field['sFieldName']]) : '0'); break; case 'char': case 'text': - if (!isset($aGridRow[$aField['sFieldName']])) { - $aGridRow[$aField['sFieldName']] = ''; + if (!isset($gridRow[$field['sFieldName']])) { + $gridRow[$field['sFieldName']] = ''; } - $sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string( - $con->getResource(), - $aGridRow[$aField['sFieldName']] - ) : '') . "'"; + $stringEscape = mysqli_real_escape_string($connection->getResource(), $gridRow[$field['sFieldName']]); + $query .= ",'" . (isset($gridRow[$field['sFieldName']]) ? $stringEscape : '') . "'"; break; case 'date': - $value = (isset($aGridRow[$aField['sFieldName']]) && trim($aGridRow[$aField['sFieldName']])) != '' ? "'" . $aGridRow[$aField['sFieldName']] . "'" : 'NULL'; - $sQuery .= "," . $value; + $value = (isset($gridRow[$field['sFieldName']]) && trim($gridRow[$field['sFieldName']])) != '' ? "'" . $gridRow[$field['sFieldName']] . "'" : 'NULL'; + $query .= "," . $value; break; } } - $sQuery .= ')'; - $rs = $stmt->executeQuery($sQuery); + $query .= ')'; + DB::insert($query); } } } - $oDataset->next(); } break; /** * For SQLServer code + * Note: It is only possible to create Report Tables in MySQL databases. The list will only show connections to those databases even if the project has connections to other DBMS. + * This section is not used and has been marked for deletion. + * @link https://wiki.processmaker.com/3.0/Report_Tables#Creating_Report_Tables + * @deprecated */ case 'mssql': - $oCriteria = new Criteria('workflow'); - $oCriteria->add(ApplicationPeer::PRO_UID, $sProcessUid); - $oCriteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER); - $oDataset = ApplicationPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - while ($aRow = $oDataset->getRow()) { - $aData = unserialize($aRow['APP_DATA']); + $criteria = new Criteria('workflow'); + $criteria->add(ApplicationPeer::PRO_UID, $proUid); + $criteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER); + $dataset = ApplicationPeer::doSelectRS($criteria); + $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataset->next(); + while ($row = $dataset->getRow()) { + $appData = unserialize($row['APP_DATA']); //verify use mssql mysqli_query( - $con->getResource(), - 'DELETE FROM [' . $sTableName . "] WHERE APP_UID = '" . $aRow['APP_UID'] . "'" + $connection->getResource(), 'DELETE FROM [' . $tableName . "] WHERE APP_UID = '" . $row['APP_UID'] . "'" ); - if ($sType == 'NORMAL') { - $sQuery = 'INSERT INTO [' . $sTableName . '] ('; - $sQuery .= '[APP_UID],[APP_NUMBER]'; - foreach ($aFields as $aField) { - $sQuery .= ',[' . $aField['sFieldName'] . ']'; + if ($type == 'NORMAL') { + $query = 'INSERT INTO [' . $tableName . '] ('; + $query .= '[APP_UID],[APP_NUMBER]'; + foreach ($fields as $field) { + $query .= ',[' . $field['sFieldName'] . ']'; } - $sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER']; - foreach ($aFields as $aField) { - switch ($aField['sType']) { + $query .= ") VALUES ('" . $row['APP_UID'] . "'," . (int) $row['APP_NUMBER']; + foreach ($fields as $field) { + switch ($field['sType']) { case 'number': - $sQuery .= ',' . (isset($aData[$aField['sFieldName']]) ? (float)str_replace( - ',', - '', - $aData[$aField['sFieldName']] - ) : '0'); + $query .= ',' . (isset($appData[$field['sFieldName']]) ? (float) str_replace(',', '', $appData[$field['sFieldName']]) : '0'); break; case 'char': case 'text': - if (!isset($aData[$aField['sFieldName']])) { - $aData[$aField['sFieldName']] = ''; + if (!isset($appData[$field['sFieldName']])) { + $appData[$field['sFieldName']] = ''; } - $sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? mysqli_real_escape_string( - $con->getResource(), - $aData[$aField['sFieldName']] - ) : '') . "'"; + $stringEscape = mysqli_real_escape_string($connection->getResource(), $appData[$field['sFieldName']]); + $query .= ",'" . (isset($appData[$field['sFieldName']]) ? $stringEscape : '') . "'"; break; case 'date': - $sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? $aData[$aField['sFieldName']] : '') . "'"; + $query .= ",'" . (isset($appData[$field['sFieldName']]) ? $appData[$field['sFieldName']] : '') . "'"; break; } } - $sQuery .= ')'; - $rs = $stmt->executeQuery($sQuery); + $query .= ')'; + $rs = $statement->executeQuery($query); } else { - if (isset($aData[$sGrid])) { - foreach ($aData[$sGrid] as $iRow => $aGridRow) { - $sQuery = 'INSERT INTO [' . $sTableName . '] ('; - $sQuery .= '`APP_UID`,`APP_NUMBER`,`ROW`'; - foreach ($aFields as $aField) { - $sQuery .= ',[' . $aField['sFieldName'] . ']'; + if (isset($appData[$grid])) { + foreach ($appData[$grid] as $indexRow => $gridRow) { + $query = 'INSERT INTO [' . $tableName . '] ('; + $query .= '`APP_UID`,`APP_NUMBER`,`ROW`'; + foreach ($fields as $field) { + $query .= ',[' . $field['sFieldName'] . ']'; } - $sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'] . ',' . $iRow; - foreach ($aFields as $aField) { - switch ($aField['sType']) { + $query .= ") VALUES ('" . $row['APP_UID'] . "'," . (int) $row['APP_NUMBER'] . ',' . $indexRow; + foreach ($fields as $field) { + switch ($field['sType']) { case 'number': - $sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace( - ',', - '', - $aGridRow[$aField['sFieldName']] - ) : '0'); + $query .= ',' . (isset($gridRow[$field['sFieldName']]) ? (float) str_replace(',', '', $gridRow[$field['sFieldName']]) : '0'); break; case 'char': case 'text': - if (!isset($aGridRow[$aField['sFieldName']])) { - $aGridRow[$aField['sFieldName']] = ''; + if (!isset($gridRow[$field['sFieldName']])) { + $gridRow[$field['sFieldName']] = ''; } - $sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string( - $con->getResource(), - $aGridRow[$aField['sFieldName']] - ) : '') . "'"; + $stringEscape = mysqli_real_escape_string($connection->getResource(), $gridRow[$field['sFieldName']]); + $query .= ",'" . (isset($gridRow[$field['sFieldName']]) ? $stringEscape : '') . "'"; break; case 'date': - $sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? $aGridRow[$aField['sFieldName']] : '') . "'"; + $query .= ",'" . (isset($gridRow[$field['sFieldName']]) ? $gridRow[$field['sFieldName']] : '') . "'"; break; } } - $sQuery .= ')'; - $rs = $stmt->executeQuery($sQuery); + $query .= ')'; + $rs = $statement->executeQuery($query); } } } - $oDataset->next(); + $dataset->next(); } break; - } } catch (Exception $oError) { throw ($oError); diff --git a/workflow/engine/src/ProcessMaker/Model/Application.php b/workflow/engine/src/ProcessMaker/Model/Application.php index eb367a03c..6b53ca048 100644 --- a/workflow/engine/src/ProcessMaker/Model/Application.php +++ b/workflow/engine/src/ProcessMaker/Model/Application.php @@ -3,6 +3,7 @@ namespace ProcessMaker\Model; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\DB; class Application extends Model { @@ -24,4 +25,31 @@ class Application extends Model { return $this->hasOne(User::class, 'APP_CUR_USER', 'USR_UID'); } + + /** + * Get Applications by PRO_UID, ordered by APP_NUMBER. + * @param string $proUid + * @return object + * @see ReportTables->populateTable() + */ + public static function getByProUid($proUid) + { + $query = Application::query() + ->select() + ->proUid($proUid) + ->orderBy('APP_NUMBER', 'ASC'); + return $query->get(); + } + + /** + * Scope for query to get the applications by PRO_UID. + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $proUid + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeProUid($query, $proUid) + { + $result = $query->where('PRO_UID', '=', $proUid); + return $result; + } }