Merge pull request #936 from MrPetovan/bug/notices
[friendica-addons.git/.git] / calc / calc.php
1 <?php
2 /**
3  * Name: Calculator App
4  * Description: Simple Calculator Application
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8 use Friendica\Core\Hook;
9 use Friendica\DI;
10
11 function calc_install() {
12         Hook::register('app_menu', 'addon/calc/calc.php', 'calc_app_menu');
13 }
14
15 function calc_uninstall() {
16         Hook::unregister('app_menu', 'addon/calc/calc.php', 'calc_app_menu');
17
18 }
19
20 function calc_app_menu($a,&$b) {
21         $b['app_menu'][] = '<div class="app-title"><a href="calc">Calculator</a></div>'; 
22 }
23
24
25 function calc_module() {}
26
27
28
29
30 function calc_init($a) {
31
32 $x = <<< EOT
33
34 <script language="JavaScript">
35 /**************************************
36  * www.FemaleNerd.com         *
37  **************************************/
38
39 // Declare global variables
40 var displayText = ""
41 var num1
42 var num2
43 var operatorType
44
45 // Write to display
46 function addDisplay(n){
47    id = document.getElementById("display");
48 id.value = ""
49 displayText += n
50 id.value = displayText
51 }
52
53 // Addition
54 function addNumbers() {
55 if (displayText == "") {
56   displayText = result
57  }
58 num1 = parseFloat(displayText)
59 operatorType = "add"
60 displayText = ""
61 }
62
63 // Subtraction
64 function subtractNumbers() {
65 if (displayText == "") {
66   displayText = result
67  }
68 num1 = parseFloat(displayText)
69 operatorType = "subtract"
70 displayText = ""
71 }
72
73 // Multiplication
74 function multiplyNumbers() {
75 if (displayText == "") {
76   displayText = result
77  }
78 num1 = parseFloat(displayText)
79 operatorType = "multiply"
80 displayText = ""
81 }
82
83 // Division
84 function divideNumbers() {
85 if (displayText == "") {
86   displayText = result
87  }
88 num1 = parseFloat(displayText)
89 operatorType = "divide"
90 displayText = ""
91 }
92
93 // Sine
94 function sin() {
95    id = document.getElementById("display");
96 if (displayText == "") {
97   num1 = result
98   }
99 else {
100   num1 = parseFloat(displayText)
101   }
102 if (num1 != "") {
103   result = Math.sin(num1)
104   id.value = result
105   displayText = ""
106   }
107 else {
108   alert("Please write the number first")
109   }
110 }
111
112 // Cosine
113 function cos() {
114    id = document.getElementById("display");
115 if (displayText == "") {
116   num1 = result
117   }
118 else {
119   num1 = parseFloat(displayText)
120   }
121 if (num1 != "") {
122   result = Math.cos(num1)
123   id.value = result
124   displayText = ""
125   }
126 else {
127   alert("Please write the number first")
128   }
129 }
130
131 // ArcSine
132 function arcSin() {
133    id = document.getElementById("display");
134 if (displayText == "") {
135   num1 = result
136   }
137 else {
138   num1 = parseFloat(displayText)
139   }
140 if (num1 != "") {
141   result = Math.asin(num1)
142   id.value = result
143   displayText = ""
144   }
145 else {
146   alert("Please write the number first")
147   }
148 }
149
150 // ArcCosine
151 function arcCos() {
152    id = document.getElementById("display");
153 if (displayText == "") {
154   num1 = result
155   }
156 else {
157   num1 = parseFloat(displayText)
158   }
159 if (num1 != "") {
160   result = Math.acos(num1)
161   id.value = result
162   displayText = ""
163   }
164 else {
165   alert("Please write the number first")
166   }
167 }
168
169 // Square root
170 function sqrt() {
171    id = document.getElementById("display");
172 if (displayText == "") {
173   num1 = result
174   }
175 else {
176   num1 = parseFloat(displayText)
177   }
178 if (num1 != "") {
179   result = Math.sqrt(num1)
180   id.value = result
181   displayText = ""
182   }
183 else {
184   alert("Please write the number first")
185   }
186 }
187
188 // Square number (number to the power of two)
189 function square() {
190    id = document.getElementById("display");
191 if (displayText == "") {
192   num1 = result
193   }
194 else {
195   num1 = parseFloat(displayText)
196   }
197 if (num1 != "") {
198   result = num1 * num1
199   id.value = result
200   displayText = ""
201   }
202 else {
203   alert("Please write the number first")
204   }
205 }
206
207 // Convert degrees to radians
208 function degToRad() {
209    id = document.getElementById("display");
210 if (displayText == "") {
211   num1 = result
212   }
213 else {
214   num1 = parseFloat(displayText)
215   }
216 if (num1 != "") {
217   result = num1 * Math.PI / 180
218   id.value = result
219   displayText = ""
220   }
221 else {
222   alert("Please write the number first")
223   }
224 }
225
226 // Convert radians to degrees
227 function radToDeg() {
228    id = document.getElementById("display");
229 if (displayText == "") {
230   num1 = result
231   }
232 else {
233   num1 = parseFloat(displayText)
234   }
235 if (num1 != "") {
236   result = num1 * 180 / Math.PI
237   id.value = result
238   displayText = ""
239   }
240 else {
241   alert("Please write the number first")
242   }
243 }
244
245 // Calculations
246 function calculate() {
247    id = document.getElementById("display");
248
249 if (displayText != "") {
250   num2 = parseFloat(displayText)
251 // Calc: Addition
252   if (operatorType == "add") {
253     result = num1 + num2
254     id.value = result
255     }
256 // Calc: Subtraction
257   if (operatorType == "subtract") {
258     result = num1 - num2
259     id.value = result
260     }
261 // Calc: Multiplication
262   if (operatorType == "multiply") {
263     result = num1 * num2
264     id.value = result
265     }
266 // Calc: Division
267   if (operatorType == "divide") {
268     result = num1 / num2
269     id.value = result
270     }
271   displayText = ""
272   }
273   else {
274   id.value = "Oops! Error!"
275   }
276 }
277
278 // Clear the display
279 function clearDisplay() {
280    id = document.getElementById("display");
281
282 displayText = ""
283 id.value = ""
284 }
285 </script>
286
287 EOT;
288 DI::page()['htmlhead'] .= $x;
289 }
290
291 function calc_content($app) {
292
293 $o = '';
294
295 $o .=  <<< EOT
296
297 <h3>Calculator</h3>
298 <br /><br />
299 <table>
300 <tbody><tr><td> 
301 <table bgcolor="#af9999" border="1">
302 <tbody><tr><td>
303 <table border="1" cellpadding="2" cellspacing="2">
304 <form name="calc">
305 <!--
306 <TR><TD VALIGN=top colspan=6 ALIGN="center"> <H2>Calculator</H2> </TD>
307 -->
308 <tbody><tr>
309         <td colspan="5"><input size="22" id="display" name="display" type="text"></td>
310 </tr><tr align="left" valign="middle">
311         <td><input name="one" value="&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;" onclick="addDisplay(1)" type="button"></td>
312         <td><input name="two" value="&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;" onclick="addDisplay(2)" type="button"></td>
313         <td><input name="three" value="&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;" onclick="addDisplay(3)" type="button"></td>
314         <td><input name="plus" value="&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;" onclick="addNumbers()" type="button"></td>
315 </tr><tr align="left" valign="middle">
316         <td><input name="four" value="&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;" onclick="addDisplay(4)" type="button"></td>
317         <td><input name="five" value="&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;" onclick="addDisplay(5)" type="button"></td>
318         <td><input name="six" value="&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;" onclick="addDisplay(6)" type="button"></td>
319         <td><input name="minus" value="&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;" onclick="subtractNumbers()" type="button"></td>
320 </tr><tr align="left" valign="middle">
321         <td><input name="seven" value="&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;" onclick="addDisplay(7)" type="button"></td>
322         <td><input name="eight" value="&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;" onclick="addDisplay(8)" type="button"></td>
323         <td><input name="nine" value="&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;" onclick="addDisplay(9)" type="button"></td>
324         <td><input name="multiplication" value="&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;" onclick="multiplyNumbers()" type="button"></td>
325 </tr><tr align="left" valign="middle">
326         <td><input name="zero" value="&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;" onclick="addDisplay(0)" type="button"></td>
327         <td><input name="pi" value="&nbsp;Pi&nbsp;&nbsp;" onclick="addDisplay(Math.PI)" type="button"> </td> 
328         <td><input name="dot" value="&nbsp;&nbsp;&nbsp;.&nbsp;&nbsp;&nbsp;" onclick='addDisplay(".")' type="button"></td>
329         <td><input name="division" value="&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;" onclick="divideNumbers()" type="button"></td>
330 </tr><tr align="left" valign="middle">
331         <td><input name="sqareroot" value="sqrt" onclick="sqrt()" type="button"></td>
332         <td><input name="squarex" value=" x^2" onclick="square()" type="button"></td>
333         <td><input name="deg-rad" value="d2r&nbsp;" onclick="degToRad()" type="button"></td>
334         <td><input name="rad-deg" value="r2d&nbsp;" onclick="radToDeg()" type="button"></td>
335 </tr><tr align="left" valign="middle">
336         <td><input name="sine" value="&nbsp;sin&nbsp;" onclick="sin()" type="button"></td>
337         <td><input name="arcsine" value="asin" onclick="arcSin()" type="button"></td>
338         <td><input name="cosine" value="cos" onclick="cos()" type="button"></td>
339         <td><input name="arccosine" value="acs" onclick="arcCos()" type="button"></td>
340
341 </tr><tr align="left" valign="middle">
342         <td colspan="2"><input name="clear" value="&nbsp;&nbsp;Clear&nbsp;&nbsp;" onclick="clearDisplay()" type="button"></td>
343         <td colspan="3"><input name="enter" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" onclick="calculate()" type="button"></td>
344
345 </tr></tbody></table>
346 </form>
347
348         <!--
349         <TD VALIGN=top> 
350                 <B>NOTE:</B> All sine and cosine calculations are
351                 <br>done in radians. Remember to convert first
352                 <br>if using degrees.
353         </TD>
354         -->
355         
356 </td></tr></tbody></table>
357
358
359 </td></tr></tbody></table>
360
361 EOT;
362 return $o;
363
364 }