Merge pull request #22115 from artfulrobot/artfulrobot-api4-count-methods
[civicrm-core.git] / CRM / Extension / Upgrader / TasksTrait.php
CommitLineData
31236900
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * The TasksTrait provides a library of tasks that are useful to run during an upgrade.
14 */
15trait CRM_Extension_Upgrader_TasksTrait {
16
17 /**
18 * @return string
19 */
20 abstract public function getExtensionDir();
21
22 /**
23 * Run a CustomData file.
24 *
25 * @param string $relativePath
26 * the CustomData XML file path (relative to this extension's dir)
27 * @return bool
28 */
29 public function executeCustomDataFile($relativePath) {
30 $xml_file = $this->getExtensionDir() . '/' . $relativePath;
31 return $this->executeCustomDataFileByAbsPath($xml_file);
32 }
33
34 /**
35 * Run a CustomData file
36 *
37 * @param string $xml_file
38 * the CustomData XML file path (absolute path)
39 *
40 * @return bool
41 */
42 protected function executeCustomDataFileByAbsPath($xml_file) {
43 $import = new CRM_Utils_Migrate_Import();
44 $import->run($xml_file);
45 return TRUE;
46 }
47
48 /**
49 * Run a SQL file.
50 *
51 * @param string $tplFile
52 * The SQL file path (relative to this extension's dir, or absolute)
53 *
54 * @return bool
55 */
56 public function executeSqlFile($tplFile) {
57 $tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->getExtensionDir() . DIRECTORY_SEPARATOR . $tplFile;
58 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $tplFile);
59 return TRUE;
60 }
61
62 /**
63 * Run the sql commands in the specified file.
64 *
65 * @param string $tplFile
66 * The SQL file path (relative to this extension's dir, or absolute).
67 * Ex: "sql/mydata.mysql.tpl".
68 *
69 * @return bool
70 * @throws \CRM_Core_Exception
71 */
72 public function executeSqlTemplate($tplFile) {
73 // Assign multilingual variable to Smarty.
74 $upgrade = new CRM_Upgrade_Form();
75
76 $tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->getExtensionDir() . DIRECTORY_SEPARATOR . $tplFile;
77 $smarty = CRM_Core_Smarty::singleton();
78 $smarty->assign('domainID', CRM_Core_Config::domainID());
79 CRM_Utils_File::sourceSQLFile(
80 CIVICRM_DSN, $smarty->fetch($tplFile), NULL, TRUE
81 );
82 return TRUE;
83 }
84
85 /**
86 * Run one SQL query.
87 *
88 * This is just a wrapper for CRM_Core_DAO::executeSql, but it
89 * provides syntactic sugar for queueing several tasks that
90 * run different queries
91 *
92 * @return bool
93 */
94 public function executeSql($query, $params = []) {
95 // FIXME verify that we raise an exception on error
96 CRM_Core_DAO::executeQuery($query, $params);
97 return TRUE;
98 }
99
100}