Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
34cd78e1 | 4 | | CiviCRM version 4.5 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright Tech To The People http:tttp.eu (c) 2008 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
11 | | Version 3, 19 November 2007. | | |
12 | | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
19 | | License along with this program; if not, contact CiviCRM LLC | | |
20 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
21 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
22 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
23 | +--------------------------------------------------------------------+ | |
24 | */ | |
25 | ||
26 | ||
27 | ||
28 | require_once ('bin/cli.php'); | |
29 | require_once 'CRM/Core/BAO/Tag.php'; | |
a1a55b61 EM |
30 | |
31 | /** | |
32 | * Class tagsImporter | |
33 | */ | |
6a488035 | 34 | class tagsImporter extends civicrm_cli { |
a1a55b61 EM |
35 | /** |
36 | * constructor | |
37 | */ | |
6a488035 TO |
38 | function __construct() { |
39 | parent::__construct(); | |
40 | if (sizeof($this->args) != 1) { | |
41 | die("you need to profide a csv file (1st column parent name, 2nd tag name"); | |
42 | } | |
43 | $this->file = $this->args[0]; | |
cd43c5e3 | 44 | $this->tags = array_flip(CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE))); |
6a488035 TO |
45 | } |
46 | ||
47 | //format expected: parent name, tag | |
48 | function run() { | |
49 | $row = 1; | |
50 | $handle = fopen($this->file, "r"); | |
51 | //header | |
52 | // $header = fgetcsv($handle, 1000, ","); | |
53 | while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | |
54 | $num = count($data); | |
55 | $row++; | |
56 | $params = $this->convertLine($data); | |
57 | $this->addTag($params); | |
58 | } | |
59 | fclose($handle); | |
60 | return; | |
61 | } | |
62 | ||
a1a55b61 EM |
63 | /** |
64 | * @param $param | |
65 | * | |
66 | * @return mixed | |
67 | */ | |
6a488035 TO |
68 | function addTag($param) { |
69 | if (array_key_exists($param['name'], $this->tags)) { | |
70 | echo "\n- exists already: " . $param['name']; | |
71 | return; | |
72 | } | |
73 | $key = array('tag' => ''); | |
74 | if ($param['parent']) { | |
75 | if (array_key_exists($param['parent'], $this->tags)) { | |
76 | $param['parent_id'] = $this->tags[$param['parent']]; | |
77 | } | |
78 | else $param['parent_id'] = $this->addTag(array(parent => '', name => $param['parent'])); | |
79 | $tag = CRM_Core_BAO_Tag::add($param, $key); | |
80 | echo "\n" . $tag->id . ": create " . $param['name'] . " below " . $param['parent']; | |
81 | } | |
82 | else { | |
83 | $tag = CRM_Core_BAO_Tag::add($param, $key); | |
84 | echo "\n" . $tag->id . ": create " . $param['name'] . " (root)"; | |
85 | } | |
86 | $this->tags[$param['name']] = $tag->id; | |
87 | return $tag->id; | |
88 | } | |
89 | ||
90 | /* return a params as expected */ | |
a1a55b61 EM |
91 | /** |
92 | * @param $data | |
93 | * | |
94 | * @return mixed | |
95 | */ | |
6a488035 TO |
96 | function convertLine($data) { |
97 | /* | |
98 | [0] => parent tag name | |
99 | [1] => name of the tag | |
100 | */ | |
101 | ||
102 | ||
103 | $params['parent'] = $data[0]; | |
104 | $params['name'] = $data[1]; | |
105 | return $params; | |
106 | } | |
107 | } | |
108 | ||
109 | $tagsImporter = new tagsImporter("civicrm_value_1_extra_information"); | |
110 | $tagsImporter->run(); | |
111 | echo "\n"; | |
112 |