File size: 8,338 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php

namespace ProcessMaker\Util;

use Configurations;
use Criteria;
use ResultSet;
use FieldsPeer;
use ReportTablePeer;
use CaseConsolidatedCorePeer;
use ConsolidatedCases;
use AdditionalTablesPeer;
use PmTable;
use ReportVarPeer;
use AdditionalTables;
use stdClass;

/**
 * This class regenerates the 'Propel' classes that are necessary for the
 * administration of a 'Report Table', this is caused by the import of processes
 * where the data directory of ProcessMaker has different routes.
 */
class FixReferencePath
{
    private $modeDebug = false;
    private $resumeDebug = "";

    /**
     * Get property modeDebug.
     *
     * @return boolean
     */
    public function getModeDebug()
    {
        return $this->modeDebug;
    }

    /**
     * Set property modeDebug.
     *
     * @param boolean $modeDebug
     */
    public function setModeDebug($modeDebug)
    {
        $this->modeDebug = $modeDebug;
    }

    /**
     * Get property resumeDebug.
     *
     * @return string
     */
    public function getResumeDebug()
    {
        return $this->resumeDebug;
    }

    /**
     * Set property resumeDebug.
     *
     * @param string $resumeDebug
     */
    public function setResumeDebug($resumeDebug)
    {
        $this->resumeDebug = $resumeDebug;
    }

    /**
     * Find all PHP type files recursively.
     * The '$pathData' argument is the path to be replaced with the path found
     * as incorrect.
     *
     * @param string $directory
     * @param string $pathData
     * @return void
     */
    public function runProcess($directory, $pathData)
    {
        try {
            //This variable is not defined and does not involve its value in this
            //task, it is removed at the end of the method.
            $_SERVER["REQUEST_URI"] = "";
            if (!defined("SYS_SKIN")) {
                $conf = new Configurations();
                define("SYS_SKIN", $conf->getConfiguration('SKIN_CRON', ''));
            }

            $criteria = new Criteria("workflow");
            $criteria->addSelectColumn(ReportTablePeer::REP_TAB_UID);
            $criteria->addSelectColumn(CaseConsolidatedCorePeer::TAS_UID);
            $criteria->addSelectColumn(ReportTablePeer::REP_TAB_NAME);
            $criteria->addJoin(ReportTablePeer::REP_TAB_UID, CaseConsolidatedCorePeer::REP_TAB_UID, Criteria::JOIN);
            $criteria->add(CaseConsolidatedCorePeer::CON_STATUS, "ACTIVE", Criteria::EQUAL);
            $doSelect = ReportTablePeer::doSelectRS($criteria);
            $doSelect->setFetchmode(ResultSet::FETCHMODE_ASSOC);

            $consolidatedCases = new ConsolidatedCases();
            while ($doSelect->next()) {
                $row = $doSelect->getRow();
                $fields = $this->getReportTableFields($row["REP_TAB_UID"]);
                list($fields, $outFields) = $consolidatedCases->buildReportVariables($fields);
                try {
                    $this->regeneratePropelClasses($row["REP_TAB_NAME"], $row["REP_TAB_NAME"], $fields, $row["TAS_UID"]);
                    $this->outVerboseln("* Regenerate classes for table: " . $row["REP_TAB_NAME"]);
                } catch (Exception $e) {
                    CLI::logging(CLI::error("Error:" . "Error in regenerate classes for table: " . $row["REP_TAB_NAME"] . ". " . $e));
                }
            }

            $criteria = new Criteria("workflow");
            $criteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID);
            $criteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME);
            $criteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_CLASS_NAME);
            $criteria->addSelectColumn(AdditionalTablesPeer::DBS_UID);
            $doSelect = AdditionalTablesPeer::doSelectRS($criteria);
            $doSelect->setFetchmode(ResultSet::FETCHMODE_ASSOC);

            while ($doSelect->next()) {
                $row = $doSelect->getRow();
                $fields = $this->getAdditionalTablesFields($row["ADD_TAB_UID"]);
                try {
                    $pmTable = new PmTable($row["ADD_TAB_NAME"]);
                    $pmTable->setDbConfigAdapter("mysql");
                    $pmTable->setColumns($fields);
                    $pmTable->prepare();
                    $pmTable->preparePropelIniFile();
                    $pmTable->buildSchema();
                    $this->outVerboseln("* Regenerate classes for table: " . $row["ADD_TAB_NAME"]);
                } catch (Exception $e) {
                    CLI::logging(CLI::error("Error:" . "Error in regenerate classes for table: " . $row["ADD_TAB_NAME"] . ". " . $e));
                }
            }

            unset($_SERVER["REQUEST_URI"]);
        } catch (Exception $e) {
            CLI::logging(CLI::error("Error:" . "Error in regenerate classes files, proceed to regenerate manually: " . $e));
        }
    }

    /**
     * Gets the fields of the 'Report Table'.
     *
     * @param string $repTabUid
     * @return array
     */
    public function getReportTableFields($repTabUid)
    {
        $fields = array();
        $criteria = new Criteria("workflow");
        $criteria->addSelectColumn(ReportVarPeer::REP_VAR_NAME);
        $criteria->addSelectColumn(ReportVarPeer::REP_VAR_TYPE);
        $criteria->add(ReportVarPeer::REP_TAB_UID, $repTabUid, Criteria::EQUAL);
        $doSelect = ReportVarPeer::doSelectRS($criteria);
        $doSelect->setFetchmode(ResultSet::FETCHMODE_ASSOC);
        while ($doSelect->next()) {
            $row = $doSelect->getRow();
            $fields[] = $row['REP_VAR_NAME'] . '-' . $row['REP_VAR_TYPE'];
        }
        return $fields;
    }

    /**
     * Gets the fields of the 'Additional Table'.
     *
     * @param string $addTabUid
     * @return object
     */
    public function getAdditionalTablesFields($addTabUid)
    {
        $fields = array();
        $criteria = new Criteria("workflow");
        $criteria->add(FieldsPeer::ADD_TAB_UID, $addTabUid);
        $doSelect = FieldsPeer::doSelectRS($criteria);
        $doSelect->setFetchmode(ResultSet::FETCHMODE_ASSOC);
        while ($doSelect->next()) {
            $row = $doSelect->getRow();
            $object = new stdClass();
            $object->field_index = $row["FLD_INDEX"];
            $object->field_name = $row["FLD_NAME"];
            $object->field_description = $row["FLD_DESCRIPTION"];
            $object->field_type = $row["FLD_TYPE"];
            $object->field_size = $row["FLD_SIZE"];
            $object->field_null = $row["FLD_NULL"];
            $object->field_autoincrement = $row["FLD_AUTO_INCREMENT"];
            $object->field_key = $row["FLD_KEY"];
            $fields[] = $object;
        }
        return $fields;
    }

    /**
     * Regenerate 'Propel' classes for 'Report Tables'. The name of the 'Report Table',
     * the fields and the related task are required.
     *
     * @param string $repTabName
     * @param array $fields
     * @param string $guid
     * @return void
     */
    public function regeneratePropelClasses($repTabName, $className, $fields, $guid)
    {
        $sourcePath = PATH_DB . config("system.workspace") . PATH_SEP . 'classes' . PATH_SEP;

        @unlink($sourcePath . $className . '.php');
        @unlink($sourcePath . $className . 'Peer.php');
        @unlink($sourcePath . PATH_SEP . 'map' . PATH_SEP . $className . 'MapBuilder.php');
        @unlink($sourcePath . PATH_SEP . 'om' . PATH_SEP . 'Base' . $className . '.php');
        @unlink($sourcePath . PATH_SEP . 'om' . PATH_SEP . 'Base' . $className . 'Peer.php');

        $additionalTables = new AdditionalTables();
        $additionalTables->createPropelClasses($repTabName, $className, $fields, $guid);
    }

    /**
     * Display the output found, the message is not displayed if the value of the
     * 'modeVerbose' property is false.
     *
     * @param string $message
     * @return void
     */
    private function outVerbose($message)
    {
        $this->resumeDebug = $this->resumeDebug . $message;
        if ($this->modeDebug === true) {
            echo $message;
        }
    }

    /**
     * Shows on the screen the output found with line break.
     *
     * @param string $message
     * @return void
     */
    private function outVerboseln($message)
    {
        $this->outVerbose($message . "\n");
    }
}